轉載請以鏈接形式標明出處:
本文出自:103style的博客
base on 3.12.0
目錄
- 前言
- OkHttpClient.newCall(Request)
- RealCall.execute()
- RealInterceptorChain.proceed(request)
- 小結
前言
前面我們對 OkHttpClient 和 Request 做了相關的介紹。
此時我們已經構建了 http客戶端
和 http請求
,接下來就好通過 http客戶端
來執行http請求
。即先通過OkHttpClient.newCall(Request)
構建RealCall
,然後通過 RealCall.execute()
來執行請求。
OkHttpClient.newCall(Request)
通過下面的源碼我們知道 OkHttpClient
的 newCall
方法即通過 RealCall.newRealCall()
構建了一個RealCall
實例,將 OkHttpClient
和 Request
賦值給實例的成員變量. 以及初始化了攔截器 RetryAndFollowUpInterceptor
.
//OkHttpClient
public Call newCall(Request request) {
return RealCall.newRealCall(this, request, false );
}
//RealCall
private RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
this.client = client;
this.originalRequest = originalRequest;
this.forWebSocket = forWebSocket;
this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client, forWebSocket);
this.timeout = new AsyncTimeout() {
@Override protected void timedOut() {
cancel();
}
};
this.timeout.timeout(client.callTimeoutMillis(), MILLISECONDS);
}
static RealCall newRealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
// Safely publish the Call instance to the EventListener.
RealCall call = new RealCall(client, originalRequest, forWebSocket);
call.eventListener = client.eventListenerFactory().create(call);
return call;
}
RealCall.execute()
public Response execute() throws IOException {
...
try {
client.dispatcher().executed(this);
Response result = getResponseWithInterceptorChain();
...
} catch (IOException e) {
...
} finally {
client.dispatcher().finished(this);
}
}
通過上面的代碼我們知道是通過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);
}
getResponseWithInterceptorChain
中依次添加了以下的攔截器,後面會具體介紹:
client.interceptors()
:我們通過OkhttpClient
添加的自定義攔截器retryAndFollowUpInterceptor
:重試及重定向攔截器BridgeInterceptor
:橋接攔截器CacheInterceptor
:緩存攔截器ConnectInterceptor
:連接攔截器client.networkInterceptors()
:網絡請求的攔截器CallServerInterceptor
:讀寫攔截器
然後將 request請求
和 interceptors
這個攔截器集合構建了一個 RealInterceptorChain
.
然後通過RealInterceptorChain.proceed(originalRequest);
返回請求結果。
RealInterceptorChain.proceed(request)
public Response proceed(...) throws IOException {
if (index >= interceptors.size()) throw new AssertionError();
calls++;
...
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);
...
return response;
}
我們可以看到這裏通過interceptor.intercept(next);
獲取的請求結果。
我們先以RetryAndFollowUpInterceptor
來介紹下。
public Response intercept(Chain chain) throws IOException {
RealInterceptorChain realChain = (RealInterceptorChain) chain;
...
response = realChain.proceed(request, streamAllocation, null, null);
...
}
看到上面代碼中的realChain.proceed(...);
方法,是不是又回到了上面的RealInterceptorChain.proceed(request)
.
所以由此可知RealInterceptorChain.proceed(request)
會 “依次” 去調用攔截器列表每個interceptors
中的interceptor.intercept(next)
,如下圖:
攔截器具體做了什麼操作呢?請聽下回分解。見 okhttp之五個攔截器的介紹
小結
通過上面的介紹,我們知道了:
OkHttpClient.newCall(Request)
構建了一個RealCall
實例。RealCall.execute()
通過添加一系列攔截器,然後依次執行攔截器的intercept(chain)
方法,然後把響應結果再一層一層回傳到給RealCall
。
以上