接上面的分析,在獲取請求response的時候,都調用瞭如下代碼
Response response = getResponseWithInterceptorChain();
我們跟進去看一下
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);
}
上面的代碼可以看到,首先添加了client.intercepors(),它們就是application 攔截器,然後添加了上篇文章介紹的五種內部攔截器,最後又添加了network攔截器
再看下面的代碼
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
originalRequest, this, eventListener, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis())
創建了RealInterceptorChain這個對象,將前面創建好的攔截器list傳入,這樣就完成了第一步。然後調用了chain的proceed()方法
@Override public Response proceed(Request request) throws IOException {
return proceed(request, streamAllocation, httpCodec, connection);
}
最終調用了四個參數的proceed方法,接着看
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
RealConnection connection) throws IOException {
if (index >= interceptors.size()) throw new AssertionError();
calls++;
// If we already have a stream, confirm that the incoming request will use it.
if (this.httpCodec != null && !this.connection.supportsUrl(request.url())) {
throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
+ " must retain the same host and port");
}
// If we already have a stream, confirm that this is the only call to chain.proceed().
if (this.httpCodec != null && calls > 1) {
throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
+ " must call proceed() exactly once");
}
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
writeTimeout);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
// Confirm that the next interceptor made its required call to chain.proceed().
if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) {
throw new IllegalStateException("network interceptor " + interceptor
+ " must call proceed() exactly once");
}
// Confirm that the intercepted response isn't null.
if (response == null) {
throw new NullPointerException("interceptor " + interceptor + " returned null");
}
if (response.body() == null) {
throw new IllegalStateException(
"interceptor " + interceptor + " returned a response with no body");
}
return response;
}
這個方法比較長,我們挑重點看
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
writeTimeout);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
上面這段代碼也創建了一個攔截器鏈,這和剛纔創建的區別在於他傳入的參數是index+1,就傳入了下一個攔截器,這樣就把我們的整個攔截器構造成了一個鏈條,設計很巧妙喔。第三塊獲取response的代碼,傳入的是剛纔構造的next攔截器鏈。
總結一下
1.創建一系列攔截器,放入一個攔截器list當中
2.創建一個攔截器鏈對象,並執行攔截器鏈的proceed方法,這裏注意,執行的是下一個攔截器的intercept方法。
這裏我們看一下intercept方法,這裏執行的是chain的proceed方法,這樣就印證了我們上面的說法,整個攔截器鏈完整的交替執行。
response = realChain.proceed(request, streamAllocation, null, null);
releaseConnection = false;
這裏,可以把OkHttp的網絡請求看成一個個攔截器的執行他的intercept方法的過程,這樣應該比較好理解了。
再次總結一下
1.在發起請求前對Request進行處理
2.調用下一個攔截器,獲取response
3.對response處理,返回給上一個攔截器