OkHttp攔截器
??攔截器是OkHttp中提供的一種強大機制,它可以實現網絡監聽、請求以及響應重寫、請求失敗重試等功能。
如上圖所示,這就OkHttp內部提供給我們的攔截器,就是當我們發起一個http請求的時候,OkHttp就會通過這個攔截器鏈來執行http請求。其中包括:
- RetryAndFollowUpInterceptor 重試和重定向攔截器
- BridgeInterceptor :橋接和適配攔截器
- CacheInterceptor :緩存攔截器
- ConnectInterceptor :鏈接攔截器
- CallServerInterceptor :請求和處理響應攔截器
BridgeInterceptor和 CacheInterceptor 主要是用來補充用戶請求創建當中缺少的一些必需的http請求頭和處理緩存的功能。
ConnectInterceptor 主要是負責建立可用的鏈接,CallServerInterceptor 主要是負責將http請求寫進網絡的IO流當中,并且從網絡IO流當中讀取服務端返回給客戶端的數據。
源碼分析
getResponseWithInterceptorChain()
上篇文章 OkHttpClient源碼分析(一)——同步、異步請求的執行流程和源碼分析 有提及到一個很重要的方法getResponseWithInterceptorChain(),同步請求的話,是在RealCall類中的excute()方法中調用到該方法,而異步請求是在RealCall的內部類AsyncCal中的excute()方法中調用,查看該方法的源碼:
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
interceptors.addAll(client.interceptors());
interceptors.add(retryAndFollowUpInterceptor);
interceptors.add(new BridgeInterceptor(client.cookieJar()));
interceptors.add(new CacheInterceptor(client.internalCache()));
interceptors.add(new ConnectInterceptor(client));
if (!forWebSocket) {
interceptors.addAll(client.networkInterceptors());
}
interceptors.add(new CallServerInterceptor(forWebSocket));
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
originalRequest, this, eventListener, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
return chain.proceed(originalRequest);
}
一、方法里初始化了一個Interceptor的集合,添加了OkHttpClient中配置的攔截器集合,然后依次添加了上述提及到的那五個攔截器;
二、創建一個攔截器鏈RealInterceptorChain,并執行攔截器鏈的proceed()方法;
查看RealInterceptorChain類的proceed()方法:
public Response proceed(Request request, StreamAllocation streamAllocation, HttpStream httpStream,
Connection connection) throws IOException {
...
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(
interceptors, streamAllocation, httpStream, connection, index + 1, request);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
...
}
其中,核心的代碼就在這里,這里再次創建了RealInterceptorChain對象,此時創建的是下一個攔截器鏈,傳入的是index + 1,并通過調用當前Interceptor的intercept()方法,將下一個攔截器鏈傳入,得到Response對象,至于攔截器的intercept()方法,下面將會分析。
RetryAndFollowUpInterceptor
主要作用是負責網絡請求失敗重連,需要注意的是,并不是所有的網絡請求失敗以后都可以進行重連,它是有一定的限制范圍的,OkHttp內部會幫我們檢測網絡異常和響應碼的判斷,如果都在它的限制范圍內的話,就會進行網絡重連。
源碼主要看intercept()方法:
@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
streamAllocation = new StreamAllocation(
client.connectionPool(), createAddress(request.url()));
int followUpCount = 0;
Response priorResponse = null;
...
}
這里創建了一個StreamAllocation對象,StreamAllocation對象是用來建立執行Http請求所需要的網絡組件的,從它名字可以看出,它是用來分配stream的,主要是用于獲取連接服務端的connection和用于與服務端進行數據傳輸的輸入輸出流 。
詳細的邏輯都在intercept()方法中的while循環中,這里不做詳細介紹,主要是介紹其中的這個:
while (true) {
...
try {
response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);
releaseConnection = false;
}
...
if (++followUpCount > MAX_FOLLOW_UPS) {
streamAllocation.release();
throw new ProtocolException("Too many follow-up requests: " + followUpCount);
}
...
}
這里我們可以發現,RealInterceptorChain調用proceed()方法,方法里又創建了一個RealInterceptorChain對象(下一個攔截器鏈 index + 1),然后通過index獲取到當前執行到的攔截器,調用攔截器的intercept()方法,這里intercept()方法中,再次調用了RealInterceptorChain的proceed()方法,形成了遞歸。
以上代碼是對重試的次數進行判斷,由此可知,并不是無限次的進行網絡重試,而是有一定的重試次數的,MAX_FOLLOW_UPS 是一個常量,值為20,也就是說最多進行20次重試,如果還不成功的話,就會釋放StreamAllocation對象和拋出ProtocolException異常。
總結:
- 創建StreamAllocation對象
- 調用RealInterceptorChain.proceed()進行網絡請求
- 根據異常結果或響應結果判斷是否要進行重新請求
- 調用下一個攔截器,對response進行處理,返回給上一個攔截器
BridgeInterceptor
同樣也是看核心方法intercept():
@Override public Response intercept(Chain chain) throws IOException {
Request userRequest = chain.request();
Request.Builder requestBuilder = userRequest.newBuilder();
...
if (userRequest.header("Host") == null) {
requestBuilder.header("Host", hostHeader(userRequest.url(), false));
}
if (userRequest.header("Connection") == null) {
requestBuilder.header("Connection", "Keep-Alive");
}
...
Response networkResponse = chain.proceed(requestBuilder.build());
...
if (transparentGzip
&& "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
&& HttpHeaders.hasBody(networkResponse)) {
GzipSource responseBody = new GzipSource(networkResponse.body().source());
Headers strippedHeaders = networkResponse.headers().newBuilder()
.removeAll("Content-Encoding")
.removeAll("Content-Length")
.build();
responseBuilder.headers(strippedHeaders);
String contentType = networkResponse.header("Content-Type");
responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));
}
return responseBuilder.build();
}
這里并沒有貼上整個方法的代碼,省略了部分,主要的操作就是為發起的Request請求添加請求頭信息,其中同樣也調用了proceed()方法遞歸調用下個攔截器,最后面是針對經過gzip壓縮過的Response進行解壓處理,這里通過判斷是否支持gzip壓縮且請求頭里面的"Content-Encoding"的value是否是"gzip"來判斷是否需要進行gzip解壓。
總結:
- 負責將用戶構建的Request請求轉化為能夠進行網絡訪問的請求;
- 將這個符合網絡請求的Request進行網絡請求;
- 將網絡請求回來的的相應Response轉化為用戶可用的Response
下一篇將為大家介紹OkHttp的緩存機制,感興趣的朋友可以繼續閱讀: