OKHttpClient简单使用工具类

public class OKhttpmanager {
    /**
     * 等义成员变量
     * OKHttpClient
     * Handler
     * 类名
     */
    private OkHttpClient mOkHttpClient;
    private static Handler handler;
    //防止多个线程同时访问 volatile
    private volatile static OKhttpmanager sManager;

    /**
     * 使用构造方法,完成初始化
     */
    private OKhttpmanager() {
        mOkHttpClient = new OkHttpClient();
        handler = new Handler();
    }
    /**
     * 使用单列模式,通过获取的方式拿到对象
     */
     public static OKhttpmanager getInstance(){
         OKhttpmanager instance = null;
         if(sManager == null){
             synchronized (OKhttpmanager.class){
                 if(instance == null){
                     instance = new OKhttpmanager();
                     sManager=instance;
                 }
             }
         }
         return instance;
     }

    /**
     * 定义接口
     */
     interface Func1 {
        void onResponse(String  result);
    }
     interface  Func2 {
         void onResponse(byte[] result);
     }
    interface  Func3 {
        void onResponse(JSONObject jsonObject);
    }
    /**
     * 使用Handler,接口,保证处理数据的逻辑在主线程
     * 处理请求网络成功的方法,返回的结果是Json字符串
     */
    private static void onSuccessJsonStringMethod(final String  jsonValue,final Func1 callBack ){
        //这里用的是Handler.post方法,把数据放到主线程中。
        handler.post(new Runnable() {
            @Override
            public void run() {
              if(callBack != null) {
                  try {
                      callBack.onResponse(jsonValue);
                  }catch (Exception e){
                      e.printStackTrace();
                  }
              }
            }
        });
    }
    //图片处理
    private static void onSuccessJsonByteMethod(final byte[] jsonValue, final Func2 callBack){
        handler.post(new Runnable() {
            @Override
            public void run() {
                if(callBack != null) {
                    try {
                        callBack.onResponse(jsonValue);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    /**
     * 暴露提供给外界调用的方法
     * 根据请求的指定的URL返回的结果是Json字符串
     */
    public void asynJsonStringByURL(String url,final Func1 callBack){
        //创建Request对象
        Request request = new Request.Builder().url(url).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //判断Response是否有对象,
                if(response != null && response.isSuccessful()){
                    onSuccessJsonStringMethod(response.body().string(),callBack);
                }
            }
        });
    }
    /**
     * 下载图片
     */
    public void asynJsonByteByURL(String url,final Func2 callBack){
        Request request = new Request.Builder().url(url).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response !=null &&response.isSuccessful()){
                 onSuccessJsonByteMethod(response.body().bytes(),callBack);
                }
            }
        });
    }



    /**
     * 提交表单
     */
    public void sendComplexForm(String url, Map<String,String> params,final Func1 callBack){
        //表单对象
        FormBody.Builder form_builder = new FormBody.Builder();
        //键值非空判断
        if(params != null && !params.isEmpty()){
            for (Map.Entry<String, String> entry: params.entrySet()){
                form_builder.add(entry.getKey(),entry.getValue());
            }
        }
        FormBody request_boby = form_builder.build();
        Request request = new Request.Builder().url(url).post(request_boby).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if(response != null && response.isSuccessful()){
                    onSuccessJsonStringMethod(response.body().string(),callBack);
                }
            }
        });


    }
}
一个简单有好用的OKhttp工具类就实现了,欢迎大家使用

            
阅读更多
换一批

没有更多推荐了,返回首页