1. 程式人生 > >封裝的OkHttpUtils,攔截器,gsonutils類,解綁(防止內記憶體洩漏)

封裝的OkHttpUtils,攔截器,gsonutils類,解綁(防止內記憶體洩漏)

OkHttpUtils類

 //定義靜態變數
    private static volatile OkHttpUtils instance=null;
    private final OkHttpClient okHttpClient;
    private Handler handler=new Handler();

    //定義構造方法
    private OkHttpUtils()
    {
        //攔截器
        okHttpClient = new OkHttpClient().newBuilder()
                .addInterceptor(new Logger())
                .build();
    }
    //定義靜態的方法建立這個類
    public static OkHttpUtils getInstance(){
        if(null==instance){
            synchronized (OkHttpUtils.class){
                if (instance==null){
                    instance=new OkHttpUtils();
                }
            }
        }
        return instance;
    }
    //定義get的請求方式
    public void doGet(String url, HashMap<String,String> map, final CallBack callBack){
        StringBuilder sb=null;
        if (map.size() == 0) {
            if (null == sb) {
                sb = new StringBuilder();
                sb.append(url);
            }
        }else{
            for(String key:map.keySet()){
                if(sb==null){
                    sb=new StringBuilder();
                    sb.append("?");
                }else{
                    sb.append("&");
                }
                sb.append(key).append("=").append(map.get(key));
            }
        }
        Request request=new Request.Builder()
                .url(url+sb.toString())
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        //非同步請求資料
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onFailure(e.getMessage());
                    }
                });

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String str = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onSuccess(str);
                    }
                });
            }
        });


    }
    //定義post的請求方式
    public void doPost(String url, HashMap<String,String> map, final CallBack callBack){
        //構建請求引數
        FormBody.Builder bodyBuilder = new FormBody.Builder();
        for (String key : map.keySet()) {
            bodyBuilder.add(key,map.get(key));
        }
        FormBody body =  bodyBuilder.build();
        Request request=new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onFailure(e.getMessage());
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String str = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onSuccess(str);
                    }
                });
            }
        });
    }
攔截器:Logger類
public class Logger implements Interceptor {
    private Request request;
    private Request original;

    @Override
    public Response intercept(Chain chain) throws IOException {
        original = chain.request();
            //取出原始資料
            HttpUrl url=original.url().newBuilder()
                    .addQueryParameter("source","android")
                    .build();
            //新增請求頭
            request = original.newBuilder()
                    .url(url)
                    .build();
        return chain.proceed(request);
    }
}
//定義介面
public interface CallBack {
    void onSuccess(Object o);
    void  onFailure(String msg);
}
//建立一個Gson類
public class GsonUtils {
    private  static Gson gson;
    public static Gson getInstance(){
        if (null==gson){
            gson=new Gson();
        }
        return gson;
    }
}

p層里加入的

 //防止記憶體洩漏
    public void detatch(){
        if (dView != null) {
            dView = null;
        }
    }
v層裡,重寫ondestory方法
//防止記憶體洩漏
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (detailPresenter != null) {
            detailPresenter.detatch();
        }
    }