下面先给出一张图,其中包含了OkHttp实现的默认拦截器,也是我们今天要分析的。
文章目录
简单示例引入
在开始我们今天的主题之前,我们还是以一个简单的例子引入,下面还是我们的老例子,执行一个简单Http请求。
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Call call = client.newCall(request);
try (Response response = call.execute()) {
return response.body().string();
}
}
RealCall的execute同步请求。
RealCall中的AsyncCall的execute异步请求。
它们都执行了Response response = getResponseWithInterceptorChain();
。
下面是该方法的代码:
这里主要是创建了一个Interceptor列表,然后创建一个Interceptor.Chain对象来处理请求并最终获取响应,那我再跟踪一下该RealInterceptorChain对象。
下面是该对象中最重要的方法,也就是处理请求的方法。(proceed方法)
这里有一个非常重要的类StreamAllocation*(由于后面涉及zhge类比较多,这里先简单介绍一下,后面的文章会单独分析):
我们要明白HTTP通信执行网络"请求"需要在"连接"上建立一个新的"流",我们将StreamAllocation称之流的桥梁,它负责为一次"请求"寻找"连接"并建立"流",从而完成远程通信。所以说StreamAllocation与"请求"、“连接”、"流"都有关。
@Override public Response proceed(Request request) throws IOException {
return proceed(request, streamAllocation, httpCodec, connection);
}
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;
}
在RealInterceptorChain.proceed()中,除了对状态及获取的reponse做检查之外,最主要的事情即是构造新的RealInterceptorChain对象,获取对应Interceptor,并调用Interceptor的intercept(next)了。在这里,index充当迭代器或指示器的角色,用于指出当前正在处理的Interceptor。
RealInterceptorChain + Interceptor实现了装饰器模式,实现了请求/响应的串式或流式处理。
接下来我们通过debug的方式展示的拦截器如下图所示.
即
- RetryAndFollowUpInterceptor
- BridgeInterceptor
- CacheInterceptor
- ConnectInterceptor
- CallServerInterceptor
通过debug的方式得到OkHttp中Http请求的执行流程入下图所示:
接下来一一揭晓: