1. 程式人生 > >Android 攔截器

Android 攔截器

LoggingInterceptor(攔截器頁面)

package com.sn.a1606bcat;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 日誌攔截器類,請求來了,先在這裡進行處理,可以得到發請求到得到請求消耗多久的時間
 * 作用:可以排查網路請求速度慢的根本原因
 *  1.有可能是我們在請求網路時,客戶端寫了一堆業務邏輯
 *  2.有可能是伺服器端,寫的有問題
 *  3.有可能就是網速不給力
 */
class LoggingInterceptor implements Interceptor {
  @Override
  public Response intercept(Interceptor.Chain chain) throws IOException {
    //拿到Request物件
    Request request = chain.request();

    long t1 = System.nanoTime();
    System.out.println(" request  = " + String.format("Sending request %s on %s%n%s",
            request.url(), chain.connection(), request.headers()));

    //拿到Response物件
    Response response = chain.proceed(request);

    long t2 = System.nanoTime();
    //得出請求網路,到得到結果,中間消耗了多長時間
    System.out.println("response  " + String.format("Received response for %s in %.1fms%n%s",
            response.request().url(), (t2 - t1) / 1e6d, response.headers()));
    return response;
  }
}

OkhtttpUtilsTwo(工具包)

package com.sn.a1606bcat;

import android.os.Handler;
import android.os.Looper;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * date:2018/11/19
 * author:(dell)
 * function:
 * 1.因為okhttp程式碼太多,太冗餘,使用的時候不是太方便,兩行程式碼搞定                                         -----doGet,doPost
 * 2.都要建立OKhttp和咱們的handler物件,物件建立太多,導致記憶體過多的消耗                                   ----單例
 * 3.非同步請求okhttp,資料請求成功以後,資料在子執行緒,所以我們還要寫handler,把資料放到主執行緒去,邏輯複雜         -----handler和介面
 *
 * 1. okhttp,handler,單例模式,介面
 *
 */
public class OkhtttpUtilsTwo {
    ////////////////////////////////////////////////單例//////////////////////////////////////
    private final Handler mHandler;
    private final OkHttpClient mOkHttpClient;
    private static OkhtttpUtilsTwo sOkhtttpUtilsTwo;

    //構造方法不私有
    private OkhtttpUtilsTwo(){
        //如果建立Handler的物件,是在一個普通的類裡建立,那麼一定要加上Looper.getMainLooper()這個引數
        mHandler = new Handler(Looper.getMainLooper());


        LoggingInterceptor loggingInterceptor = new LoggingInterceptor();

        mOkHttpClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .readTimeout(5000, TimeUnit.MILLISECONDS)
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000, TimeUnit.MILLISECONDS)
                .build();
    }

    //單例暴露一個普通的方法,給對方,雙重鎖模式
    public static OkhtttpUtilsTwo getInstance(){
        if (sOkhtttpUtilsTwo  ==null){
            synchronized (OkhtttpUtilsTwo.class){
                if (sOkhtttpUtilsTwo == null){
                    return sOkhtttpUtilsTwo =new OkhtttpUtilsTwo();
                }
            }
        }
        return sOkhtttpUtilsTwo;
    }
    //////////////////////////////////////////////////////介面////////////////////////////////
    public interface OkCallback {
        void onFailure (Exception e);
        void onResponse(String json);
    }
    ////////////////////////////////////////////okhttp與handler///////////////////////////
    /////封裝doGEt的網路封裝,引數定義兩個,一個是URL網址   一個實現介面的物件
    public void doGet(String url , final OkCallback ycfOkCallback){
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                if (ycfOkCallback !=null){
                    //切換到主執行緒
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            ycfOkCallback.onFailure(e);
                        }
                    });
                }
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response !=null && response.isSuccessful()){
                    final String json = response.body().string();
                    if (ycfOkCallback !=null){
                        //切換到主執行緒
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                ycfOkCallback.onResponse(json);
                            }
                        });
                    }
                }

            }
        });
    }

    //封裝doPost的邏輯程式碼
    public void doPost(String url, Map<String,String> map, final OkCallback ycfOkCallback){
        //建立FormBody物件,把表單新增到FormBody
        FormBody.Builder builder = new FormBody.Builder();
        //集合物件不為null的情況下
        if (map != null){
            for(String key: map.keySet()){
                builder.add(key,map.get(key));
            }
        }

        FormBody formBody = builder.build();

        //建立Request物件
        Request request = new Request.Builder()
                .post(formBody)
                .url(url)
                .build();

        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                if (ycfOkCallback !=null){
                    //切換到主執行緒
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            ycfOkCallback.onFailure(e);
                        }
                    });
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response !=null && response.isSuccessful()){
                    final String json = response.body().string();
                    if (ycfOkCallback !=null){
                        //切換到主執行緒
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                ycfOkCallback.onResponse(json);
                            }
                        });
                    }
                }
            }
        });

    }



}