Retrofit Interceptor(攔截器) 攔截請求并做相關處理

本文介紹Retrofit攔截器(Interceptor)的使用方法及相關注意事項

首先看一下Interceptor源碼:

/**
* Observes, modifies, and potentially short-circuits requests going out and the corresponding
* responses coming back in. Typically interceptors add, remove, or transform headers on the request
* or response.
*/

public interface Interceptor {
    Response intercept(Chain chain) throws IOException;

    interface Chain {
        Request request();

        Response proceed(Request request) throws IOException;

        /**
         * Returns the connection the request will be executed on. This is only available in the chains
         * of network interceptors; for application interceptors this is always null.
         */
        @Nullable Connection connection();
    }
}

先看一下api描述,翻譯過來其實就是可以通過攔截器攔截即將發(fā)出的請求及對響應結果做相應處理,典型的處理方式是修改header。其實我們可能不僅要處理header,有時也需要添加統(tǒng)一參數(shù),都可以在攔截器內(nèi)部完成。

看一下Interceptor接口,只有intercept(Chain chain)方法,其返回值是Response,顧名思義,是響應數(shù)據(jù),我們要做的也就是重寫該方法以達到我們的目的。intercept(Chain chain)方法中有個Chain參數(shù),Chain是Interceptor接口內(nèi)部中定義的另一個接口,我們暫且不管Retrofit內(nèi)部是如何實現(xiàn)該接口的(這部分內(nèi)容將會在新的文章中統(tǒng)一講解),現(xiàn)在只需要知道調(diào)用其request()方法可以拿到Request,調(diào)用其proceed(Request request)方法可以得到相應數(shù)據(jù)即可。

到此為止,Interceptor基本用法已經(jīng)知曉,下面上示例代碼:

 public class CommonInterceptor implements Interceptor {

    private static Map<String, String> commonParams;

    public synchronized static void setCommonParam(Map<String, String> commonParams) {
        if (commonParams != null) {
            if (CommonInterceptor.commonParams != null) {
                CommonInterceptor.commonParams.clear();
            } else {
                CommonInterceptor.commonParams = new HashMap<>();
            }
            for (String paramKey : commonParams.keySet()) {
                CommonInterceptor.commonParams.put(paramKey, commonParams.get(paramKey));
            }
        }
    }

    public synchronized static void updateOrInsertCommonParam(@NonNull String paramKey, @NonNull String paramValue) {
        if (commonParams == null) {
            commonParams = new HashMap<>();
        }
        commonParams.put(paramKey, paramValue);
    }

    @Override
    public synchronized Response intercept(Chain chain) throws IOException {
        Request request = rebuildRequest(chain.request());
        Response response = chain.proceed(request);
        // 輸出返回結果
        try {
            Charset charset;
            charset = Charset.forName("UTF-8");
            ResponseBody responseBody = response.peekBody(Long.MAX_VALUE);
            Reader jsonReader = new InputStreamReader(responseBody.byteStream(), charset);
            BufferedReader reader = new BufferedReader(jsonReader);
            StringBuilder sbJson = new StringBuilder();
            String line = reader.readLine();
            do {
                sbJson.append(line);
                line = reader.readLine();
            } while (line != null);
            LogUtil.e("response: " + sbJson.toString());
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e(e.getMessage(), e);
        }
//        saveCookies(response, request.url().toString());
        return response;
    }


    public static byte[] toByteArray(RequestBody body) throws IOException {
        Buffer buffer = new Buffer();
        body.writeTo(buffer);
        InputStream inputStream = buffer.inputStream();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] bufferWrite = new byte[4096];
        int n;
        while (-1 != (n = inputStream.read(bufferWrite))) {
            output.write(bufferWrite, 0, n);
        }
        return output.toByteArray();
    }

    private Request rebuildRequest(Request request) throws IOException {
        Request newRequest;
        if ("POST".equals(request.method())) {
            newRequest = rebuildPostRequest(request);
        } else if ("GET".equals(request.method())) {
            newRequest = rebuildGetRequest(request);
        } else {
            newRequest = request;
        }
        LogUtil.e("requestUrl: " + newRequest.url().toString());
        return newRequest;
    }

    /**
     * 對post請求添加統(tǒng)一參數(shù)
     */
    private Request rebuildPostRequest(Request request) {
//        if (commonParams == null || commonParams.size() == 0) {
//            return request;
//        }
        Map<String, String> signParams = new HashMap<>(); // 假設你的項目需要對參數(shù)進行簽名
        RequestBody originalRequestBody = request.body();
        assert originalRequestBody != null;
        RequestBody newRequestBody;
        if (originalRequestBody instanceof FormBody) { // 傳統(tǒng)表單
            FormBody.Builder builder = new FormBody.Builder();
            FormBody requestBody = (FormBody) request.body();
            int fieldSize = requestBody == null ? 0 : requestBody.size();
            for (int i = 0; i < fieldSize; i++) {
                builder.add(requestBody.name(i), requestBody.value(i));
                signParams.put(requestBody.name(i), requestBody.value(i));
            }
            if (commonParams != null && commonParams.size() > 0) {
                signParams.putAll(commonParams);
                for (String paramKey : commonParams.keySet()) {
                    builder.add(paramKey, commonParams.get(paramKey));
                }
            }
            // ToDo 此處可對參數(shù)做簽名處理 signParams
            /**
             * String sign = SignUtil.sign(signParams);
             * builder.add("sign", sign);
             */
            newRequestBody = builder.build();
        } else if (originalRequestBody instanceof MultipartBody) { // 文件
            MultipartBody requestBody = (MultipartBody) request.body();
            MultipartBody.Builder multipartBodybuilder = new MultipartBody.Builder();
            if (requestBody != null) {
                for (int i = 0; i < requestBody.size(); i++) {
                    MultipartBody.Part part = requestBody.part(i);
                    multipartBodybuilder.addPart(part);

                    /*
                     上傳文件時,請求方法接收的參數(shù)類型為RequestBody或MultipartBody.Part參見ApiService文件中uploadFile方法
                     RequestBody作為普通參數(shù)載體,封裝了普通參數(shù)的value; MultipartBody.Part即可作為普通參數(shù)載體也可作為文件參數(shù)載體
                     當RequestBody作為參數(shù)傳入時,框架內(nèi)部仍然會做相關處理,進一步封裝成MultipartBody.Part,因此在攔截器內(nèi)部,
                     攔截的參數(shù)都是MultipartBody.Part類型
                     */

                    /*
                     1.若MultipartBody.Part作為文件參數(shù)載體傳入,則構造MultipartBody.Part實例時,
                     需使用MultipartBody.Part.createFormData(String name, @Nullable String filename, RequestBody body)方法,
                     其中name參數(shù)可作為key使用(因為你可能一次上傳多個文件,服務端可以此作為區(qū)分)且不能為null,
                     body參數(shù)封裝了包括MimeType在內(nèi)的文件信息,其實例創(chuàng)建方法為RequestBody.create(final @Nullable MediaType contentType, final File file)
                     MediaType獲取方式如下:
                     String fileType = FileUtil.getMimeType(file.getAbsolutePath());
                     MediaType mediaType = MediaType.parse(fileType);

                     2.若MultipartBody.Part作為普通參數(shù)載體,建議使用MultipartBody.Part.createFormData(String name, String value)方法創(chuàng)建Part實例
                       name可作為key使用,name不能為null,通過這種方式創(chuàng)建的實例,其RequestBody屬性的MediaType為null;當然也可以使用其他方法創(chuàng)建
                     */

                    /*
                      提取非文件參數(shù)時,以RequestBody的MediaType為判斷依據(jù).
                      此處提取方式簡單暴力。默認part實例的RequestBody成員變量的MediaType為null時,part為非文件參數(shù)
                      前提是:
                      a.構造RequestBody實例參數(shù)時,將MediaType設置為null
                      b.構造MultipartBody.Part實例參數(shù)時,推薦使用MultipartBody.Part.createFormData(String name, String value)方法,或使用以下方法
                        b1.MultipartBody.Part.create(RequestBody body)
                        b2.MultipartBody.Part.create(@Nullable Headers headers, RequestBody body)
                        若使用方法b1或b2,則要求

                      備注:
                      您也可根據(jù)需求修改RequestBody的MediaType,但盡量保持外部傳入?yún)?shù)的MediaType與攔截器內(nèi)部添加參數(shù)的MediaType一致,方便統(tǒng)一處理
                     */

                    MediaType mediaType = part.body().contentType();
                    if (mediaType == null) {
                        String normalParamKey;
                        String normalParamValue;
                        try {
                            normalParamValue = getParamContent(requestBody.part(i).body());
                            Headers headers = part.headers();
                            if (!TextUtils.isEmpty(normalParamValue) && headers != null) {
                                for (String name : headers.names()) {
                                    String headerContent = headers.get(name);
                                    if (!TextUtils.isEmpty(headerContent)) {
                                        String[] normalParamKeyContainer = headerContent.split("name=\"");
                                        if (normalParamKeyContainer.length == 2) {
                                            normalParamKey = normalParamKeyContainer[1].split("\"")[0];
                                            signParams.put(normalParamKey, normalParamValue);
                                            break;
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            if (commonParams != null && commonParams.size() > 0) {
                signParams.putAll(commonParams);
                for (String paramKey : commonParams.keySet()) {
                    // 兩種方式添加公共參數(shù)
                    // method 1
                    multipartBodybuilder.addFormDataPart(paramKey, commonParams.get(paramKey));
                    // method 2
//                    MultipartBody.Part part = MultipartBody.Part.createFormData(paramKey, commonParams.get(paramKey));
//                    multipartBodybuilder.addPart(part);
                }
            }
            // ToDo 此處可對參數(shù)做簽名處理 signParams
            /**
             * String sign = SignUtil.sign(signParams);
             * multipartBodybuilder.addFormDataPart("sign", sign);
             */
            newRequestBody = multipartBodybuilder.build();
        } else {
            try {
                JSONObject jsonObject;
                if (originalRequestBody.contentLength() == 0) {
                    jsonObject = new JSONObject();
                } else {
                    jsonObject = new JSONObject(getParamContent(originalRequestBody));
                }
                if (commonParams != null && commonParams.size() > 0) {
                    for (String commonParamKey : commonParams.keySet()) {
                        jsonObject.put(commonParamKey, commonParams.get(commonParamKey));
                    }
                }
                // ToDo 此處可對參數(shù)做簽名處理
                /**
                 * String sign = SignUtil.sign(signParams);
                 * jsonObject.put("sign", sign);
                 */
                newRequestBody = RequestBody.create(originalRequestBody.contentType(), jsonObject.toString());
                LogUtil.e(getParamContent(newRequestBody));

            } catch (Exception e) {
                newRequestBody = originalRequestBody;
                e.printStackTrace();
            }
        }
//        可根據(jù)需求添加或修改header,此處制作示意
//       return request.newBuilder()
//                .addHeader("header1", "header1")
//                .addHeader("header2", "header2")
//                .method(request.method(), newRequestBody)
//                .build();
        return request.newBuilder().method(request.method(), newRequestBody).build();
    }

    /**
     * 獲取常規(guī)post請求參數(shù)
     */
    private String getParamContent(RequestBody body) throws IOException {
        Buffer buffer = new Buffer();
        body.writeTo(buffer);
        return buffer.readUtf8();
    }

    /**
     * 對get請求做統(tǒng)一參數(shù)處理
     */
    private Request rebuildGetRequest(Request request) {
        if (commonParams == null || commonParams.size() == 0) {
            return request;
        }
        String url = request.url().toString();
        int separatorIndex = url.lastIndexOf("?");
        StringBuilder sb = new StringBuilder(url);
        if (separatorIndex == -1) {
            sb.append("?");
        }
        for (String commonParamKey : commonParams.keySet()) {
            sb.append("&").append(commonParamKey).append("=").append(commonParams.get(commonParamKey));
        }
        Request.Builder requestBuilder = request.newBuilder();
        return requestBuilder.url(sb.toString()).build();
    }
}

該攔截器示例代碼提供了插入公共參數(shù)及對添加header功能(該功能在代碼中被注釋掉,如需要,放開即可)。對Request的攔截處理在rebuildRequest(Request request) 方法中完成,該方法只處理了GET與POST請求,內(nèi)部有較為詳盡的注釋,較為復雜的是文件傳輸,有些需要注意的事項也做了盡可能完善的說明;對響應數(shù)據(jù)的處理,代碼示例中只做了結果輸出處理,僅僅做個示范。

攔截器部分沒有過多需要做說明的地方,比較簡單,本文的示例可直接使用。如有疑問,歡迎留言。
后續(xù)將抽時間,對Retrofit做流程上的簡單梳理,了解各個配置及部分細節(jié)實現(xiàn),比如該文中的Chain實例
完整示例:https://github.com/670832188/TestApp

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 又是一年中秋佳節(jié),祝各位中秋節(jié)快樂。 今天我們來聊聊這個最近很火的網(wǎng)絡請求庫retrofit,在此基礎上會延伸出一...
    涅槃1992閱讀 7,939評論 13 133
  • 苦心鉆研6天以及各路朋友的幫助下終于有了成果,安卓6.0系統(tǒng)重啟后也可以修改有關有線網(wǎng)的IP,網(wǎng)關地址等。 在這里...
    piao先生1920閱讀 3,201評論 0 2
  • 蜘蛛在墻角不遺余力地吐絲結網(wǎng), 蛛網(wǎng)尚有經(jīng)緯,心緒卻無紋理。 十月的湖風,裹著鹽粒,徑直吹來 殘存的油菜,招搖身姿...
    菜瓜飯閱讀 326評論 0 3
  • 這里不僅銷售雜貨,還提供煩惱咨詢,無論你掙扎還是猶豫,絕望還是痛苦,浪矢雜貨店都能為你排憂解難!------《解憂...
    知行9閱讀 409評論 0 1

友情鏈接更多精彩內(nèi)容