これは...Excute()プロセスの紹介
5589 ワード
転載はリンク形式で出典を明記してください:本文は103 styleのブログから出ています
base on
目次前言 OkHttpClient.newCall(Request) RealCall.execute() RealInterceptorChain.proceed(request) 小結 前言
先にOkHttpClientとRequestについて紹介しました.
このとき、
OkHttpClient.newCall(Request)
RealCall.execute()
上記のコードから、 そして、
RealInterceptorChain.proceed(request)
ここで
上のコードの
したがって、
遮断器は具体的にどんな操作をしましたか?次回の分解を聞いてください.okhttpの5つのブロックの紹介を参照してください
小結
以上の紹介を通じて、私たちは知っています.
以上
base on
3.12.0
目次
先に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 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)
は、以下の図:遮断器は具体的にどんな操作をしましたか?次回の分解を聞いてください.okhttpの5つのブロックの紹介を参照してください
小結
以上の紹介を通じて、私たちは知っています.
interceptors
は、interceptor.intercept(next)
のインスタンスを構築する.OkHttpClient.newCall(Request)
は、一連のブロッキングを追加することによって、次いで、ブロッキングのRealCall
方法を順次実行し、応答結果をRealCall.execute()
にさらに1層ずつ返信する.以上