首页 > 代码库 > okHttp的二次封装

okHttp的二次封装

主要封装了post方式及get请求方式,并把okHttp的子线程结果直接运行在主线程中。

一般情况的顺序,okHttp → json串 → process(json)在主线程中把json赋值给view

把上面顺序封装起来,暴露出json 结果给 UIThread

package com.z.myapplication;
import com.google.gson.internal.$Gson$Types;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

//泛型T: 代表可以返回给主线程 什么类型的javaBean,比如是person 还是animal
public abstract class MyHttpCallback<T> {
    Type type;
    static Type getSuperclassTypeParameter(Class<?> subclass){
        Type superclass =subclass.getGenericSuperclass();
        if(superclass instanceof Class){
            throw new RuntimeException("Missing type parameter");

        }
        ParameterizedType parameterized=(ParameterizedType)superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }
    public MyHttpCallback(){
         type=getSuperclassTypeParameter(getClass());
    }
    public abstract void onBeforeRequest(Request request);
    public abstract void onFailure(Call call, IOException e);
    public abstract void onSuccess(Response response, T t);
    public abstract void one rror(Response response,String errorMsg);
}

 

 

package com.z.myapplication;
import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpManager {
    private OkHttpClient client;
    private Handler handler;
    private  Gson gson;
    private  static HttpManager httpmanager;
    private HttpManager(){
        //okhhtp的单例模式
        client=new OkHttpClient();
        gson = new Gson();
        //构造一个handler,不管是从哪个哪个线程中发出消息,
        // 消息都会发送到主线程的messagequeue中
        handler=new Handler(Looper.getMainLooper());

    }
    public static HttpManager getInstance(){
        if(httpmanager==null){
            synchronized (HttpManager.class){
                httpmanager=new HttpManager();
            }
        }
        return httpmanager;
    }
    //okHttp网络操作的方法
    //写一个方法供给request 给okHttpclient使用
    private void doRequest(Request request,final MyHttpCallback callback){
        callback.onBeforeRequest(request);
        client.newCall(request).enqueue(new Callback() {
           //这些okhttp的回调方法执行在子线程里面,返回的数据为了给主线程使用,
           // 必须想办法把数据供给主线程使用,所以引用了自定义的回调接口
           @Override
           public void onFailure(final Call call, final IOException e) {
               handler.post(new Runnable() {
                   @Override
                   public void run() {
                    callback.onFailure(call,e);
                   }
               });
           }
           @Override
           public void onResponse(Call call,final Response response) throws IOException {
              if(response.isSuccessful()){
                  //返回数据成功的话就解析json串
                  String json=response.body().string();
                  final Object o=gson.fromJson(json,callback.type);//将json解析成对应的bean
                  //handler 除了可以发送Message 也可以发送 runnable对象
                  //这个runnable对象的run方法就执行在 handler对应的线程中 当前的案例中 会执行在主线程中
                  handler.post(new Runnable() {
                      @Override
                      public void run() {
                          //将response返回给主线程
                          callback.onSuccess(response,o);
                      }
                  });
              }else{
                  handler.post(new Runnable() {
                      @Override
                      public void run() {
                          callback.onError(response,"服务器返回错误");
                      }
                  });
              }
           }
       });
    }
    //GET方式返回数据,通过回调返回主线程数据
    public void doGet(String url,MyHttpCallback callback){
        Request request= new Request.Builder().url(url).get().build();
        doRequest(request,callback);
    }
    //post方法. username  password
    public void doPost(String url, Map<String ,String>params,MyHttpCallback  callback){
        FormBody.Builder formBuilder=new FormBody.Builder();
        if(params!=null){
            for (Map.Entry<String,String> entry:params.entrySet()) {
               formBuilder.add(entry.getKey(),entry.getValue());
            }
        }
        RequestBody body=formBuilder.build();
        Request request=new Request.Builder().url(url).post(body).build();
        doRequest(request,callback);
    }
}

 

okHttp的二次封装