您好,登錄后才能下訂單哦!
本文介紹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描述,翻譯過來其實就是可以通過攔截器攔截即將發出的請求及對響應結果做相應處理,典型的處理方式是修改header。其實我們可能不僅要處理header,有時也需要添加統一參數,都可以在攔截器內部完成。
看一下Interceptor接口,只有intercept(Chain chain)方法,其返回值是Response,顧名思義,是響應數據,我們要做的也就是重寫該方法以達到我們的目的。intercept(Chain chain)方法中有個Chain參數,Chain是Interceptor接口內部中定義的另一個接口,我們暫且不管Retrofit內部是如何實現該接口的(這部分內容將會在新的文章中統一講解),現在只需要知道調用其request()方法可以拿到Request,調用其proceed(Request request)方法可以得到相應數據即可。
到此為止,Interceptor基本用法已經知曉,下面上示例代碼:
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請求添加統一參數 */ private Request rebuildPostRequest(Request request) { // if (commonParams == null || commonParams.size() == 0) { // return request; // } Map<String, String> signParams = new HashMap<>(); // 假設你的項目需要對參數進行簽名 RequestBody originalRequestBody = request.body(); assert originalRequestBody != null; RequestBody newRequestBody; if (originalRequestBody instanceof FormBody) { // 傳統表單 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 此處可對參數做簽名處理 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); /* 上傳文件時,請求方法接收的參數類型為RequestBody或MultipartBody.Part參見ApiService文件中uploadFile方法 RequestBody作為普通參數載體,封裝了普通參數的value; MultipartBody.Part即可作為普通參數載體也可作為文件參數載體 當RequestBody作為參數傳入時,框架內部仍然會做相關處理,進一步封裝成MultipartBody.Part,因此在攔截器內部, 攔截的參數都是MultipartBody.Part類型 */ /* 1.若MultipartBody.Part作為文件參數載體傳入,則構造MultipartBody.Part實例時, 需使用MultipartBody.Part.createFormData(String name, @Nullable String filename, RequestBody body)方法, 其中name參數可作為key使用(因為你可能一次上傳多個文件,服務端可以此作為區分)且不能為null, body參數封裝了包括MimeType在內的文件信息,其實例創建方法為RequestBody.create(final @Nullable MediaType contentType, final File file) MediaType獲取方式如下: String fileType = FileUtil.getMimeType(file.getAbsolutePath()); MediaType mediaType = MediaType.parse(fileType); 2.若MultipartBody.Part作為普通參數載體,建議使用MultipartBody.Part.createFormData(String name, String value)方法創建Part實例 name可作為key使用,name不能為null,通過這種方式創建的實例,其RequestBody屬性的MediaType為null;當然也可以使用其他方法創建 */ /* 提取非文件參數時,以RequestBody的MediaType為判斷依據. 此處提取方式簡單暴力。默認part實例的RequestBody成員變量的MediaType為null時,part為非文件參數 前提是: a.構造RequestBody實例參數時,將MediaType設置為null b.構造MultipartBody.Part實例參數時,推薦使用MultipartBody.Part.createFormData(String name, String value)方法,或使用以下方法 b1.MultipartBody.Part.create(RequestBody body) b2.MultipartBody.Part.create(@Nullable Headers headers, RequestBody body) 若使用方法b1或b2,則要求 備注: 您也可根據需求修改RequestBody的MediaType,但盡量保持外部傳入參數的MediaType與攔截器內部添加參數的MediaType一致,方便統一處理 */ 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()) { // 兩種方式添加公共參數 // method 1 multipartBodybuilder.addFormDataPart(paramKey, commonParams.get(paramKey)); // method 2 // MultipartBody.Part part = MultipartBody.Part.createFormData(paramKey, commonParams.get(paramKey)); // multipartBodybuilder.addPart(part); } } // ToDo 此處可對參數做簽名處理 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 此處可對參數做簽名處理 /** * 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(); } } // 可根據需求添加或修改header,此處制作示意 // return request.newBuilder() // .addHeader("header1", "header1") // .addHeader("header2", "header2") // .method(request.method(), newRequestBody) // .build(); return request.newBuilder().method(request.method(), newRequestBody).build(); } /** * 獲取常規post請求參數 */ private String getParamContent(RequestBody body) throws IOException { Buffer buffer = new Buffer(); body.writeTo(buffer); return buffer.readUtf8(); } /** * 對get請求做統一參數處理 */ 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(); } }
該攔截器示例代碼提供了插入公共參數及對添加header功能(該功能在代碼中被注釋掉,如需要,放開即可)。對Request的攔截處理在rebuildRequest(Request request) 方法中完成,該方法只處理了GET與POST請求,內部有較為詳盡的注釋,較為復雜的是文件傳輸,有些需要注意的事項也做了盡可能完善的說明;對響應數據的處理,代碼示例中只做了結果輸出處理,僅僅做個示范。
攔截器部分沒有過多需要做說明的地方,比較簡單,本文的示例可直接使用。如有疑問,歡迎留言。
后續將抽時間,對Retrofit做流程上的簡單梳理,了解各個配置及部分細節實現,比如該文中的Chain實例
完整示例: https://github.com/670832188/TestApp
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。