xUtils3的使用


MainActivity 主要是调用xutils3的各个方法

在导包那里找到
org.xutils:xutils:3.5.0

可以获取网络上的数据(get,post)
可以获取图片


package com.example.lixin.xutils;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    @ViewInject(R.id.iv)
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        getImage();
    }

    //方法必须是private
    @Event(R.id.test_click_btn)
    private void TestOnclick(View view) {
        Toast.makeText(this, "我通过envet注释的方式点击", Toast.LENGTH_SHORT).show();
    }


    @Event(R.id.test_post_btn)
    private void TestPost(View view) {

        String url = "http://v.juhe.cn/movie/movies.today";

        RequestParams params = new RequestParams(url);
        params.addBodyParameter("cityid", "2");
        params.addBodyParameter("key", "6faab85464a03e5d8c736cb5e4cd5118");
        params.addHeader("head","android");

        x.http().post(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Toast.makeText(MainActivity.this, "post测试成功"+result, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    @Event(type = View.OnLongClickListener.class, value = R.id.test_get_btn)
    private boolean TestGet(View view) {
        String url = "http://v.juhe.cn/movie/movies.today";
        final RequestParams params = new RequestParams(url);
        params.addQueryStringParameter("cityid", "2");
        params.addQueryStringParameter("key", "6faab85464a03e5d8c736cb5e4cd5118");

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("----------------------------"+result);
                Gson gson = new Gson();
                bean bean = gson.fromJson(result, bean.class);
                List<com.example.lixin.xutils.bean.ResultBean> list = bean.getResult();
                String name = list.get(0).getMovieName();
                Toast.makeText(MainActivity.this, "get测试成功"+name, Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });
        return true;
    }
    public void getImage(){

        ImageOptions options = new ImageOptions.Builder()
//                .setFadeIn(true)
                  .setCircular(true) //设置图片显示为圆形
//                .setSquare(true) //设置图片显示为正方形
//                .setCrop(true).setSize(200,200) //设置大小
//                .setAnimation(animation) //设置动画
//                .setFailureDrawable(Drawable failureDrawable) //设置加载失败的动画
//                .setFailureDrawableId(int failureDrawable) //以资源id设置加载失败的动画
//                .setLoadingDrawable(Drawable loadingDrawable) //设置加载中的动画
//                .setLoadingDrawableId(int loadingDrawable) //以资源id设置加载中的动画
//                .setIgnoreGif(false) //忽略Gif图片
//                .setParamsBuilder(ParamsBuilder paramsBuilder) //在网络请求中添加一些参数
//                .setRaduis(int raduis) //设置拐角弧度
//                .setUseMemCache(true) //设置使用MemCache,默认true
                .setCrop(true)
                .setSize(800,800)
                .setUseMemCache(true)
                .build();
        x.image().bind(iv,"http://imgsrc.baidu.com/imgad/pic/item/267f9e2f07082838b5168c32b299a9014c08f1f9.jpg",options);
    }
}







 MyApplication

配置这个
import android.app.Application;
import org.xutils.x;

/**
 * Created by hua on 2017/8/2.
 */

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
    }
}

你可能感兴趣的:(xUtils3的使用)