XUtils的使用方法

加上依赖

    compile 'org.xutils:xutils:3.5.0'
    //这是Gson包的依赖
    compile 'com.google.code.gson:gson:2.2.1'

自定义Class用来声明xUtils

package com.example.interfacetext;

import android.app.Application;

import org.xutils.x;

/**
 * Created by Administrator on 2017/11/3 0003.
 */
//用来声明xUtils,记得在AndroidManifest中声明name = 类名
public class BaseApplication extends Application{
    @Override
    public void onCreate() {
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG);
    }
}

自定义的View的使用Xutils

package com.example.interfacetext;

import android.util.Log;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;

/**
 * Created by Administrator on 2017/11/3 0003.
 */
//专门定义一个访问网络的类
public class HttpUtils {
    //定义一个本类的变量
    private IResponse iResponse;
    //将接口绑定到这个方法
    public void setIRspose(IResponse iiResponse){
        this.iResponse = iiResponse;
    }
    //将对象声明成私有的
    //TODO:声明instence的意义何在??
    private static volatile HttpUtils instence;//= new HttpUtils()饿汉式;
    //定义一个私有的构造方法
    private HttpUtils(){};
    //给外部提供一个获取对象的方法
    /*
    * 双重检验锁的单例模式
    * */
    //TODO:返回的instence是什么?定义这个方法为什么就能调用get方法??
    public static HttpUtils getInstence()
    {
        //判断instence是否存在
        if(instence == null)
        {
            //线程不安全,加上同步锁
            synchronized (HttpUtils.class){
                if(null == instence)
                {
                    //实例化这个类
                    instence = new HttpUtils();
                }
            }
        }
        return instence;
    }
    public void get(String url, Map<String,String> parmas, final IResponse iResponse)
    {
        //需要传一个地址所以实例化一个RequestParams传一个地址
        RequestParams requestParams = new RequestParams(url);
        /*
        * Map的存放是Map<String,String> map = new Map<>();
        * map.put("mobile","15701207385")
        * map.put(entry.getKey(),entry.getValue())
        * */
        //TODO:Map集合怎么就有值了?解决:在主方法声明一个map集合放进输入的值
        for(Map.Entry<String,String> entry:parmas.entrySet())
        {
            /*
            * //传入两个参数
                requestParams.addQueryStringParameter("mobile",username);
                requestParams.addQueryStringParameter("password",password);
            * */
            requestParams.addQueryStringParameter(entry.getKey(),entry.getValue());
        }

        //进行访问网络的方法,自动生成的四个方法,同时要设置访问网络的两个权限
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("-----","登录成功"+result);
                //调用接口里的成功方法
                iResponse.success(result);
            }

            @Override
                //网络请求错误时执行
            public void onError(Throwable ex, boolean isOnCallback) {
                //调用接口里的失败方法
                iResponse.failed(ex.getMessage());

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/dealpoor/article/details/78453998