Okhttp的Interceptor拦截器源码解析
OkHttp的调用流程:
在调用client.newCall(request)将会调用到RealCall.newRealCall(this, request, false);方法,之后将会调用到RealCall.getResponseWithInterceptorChain()函数,而在其中将进行拦截器链的构建。
RealCall.java
核心代码:
Response result = getResponseWithInterceptorChain();
通过getResponseWithInterceptorChain完成对拦截器的构建
@Override public Response execute() throws IOException {
synchronized (this) {
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
captureCallStackTrace();
try {
client.dispatcher().executed(this);
Response result = getResponseWithInterceptorChain();
if (result == null) throw new IOException("Canceled");
return result;
} finally {
client.dispatcher().finished(this);
}
}
getResponseWithInterceptorChain()
包含自定义拦截器
重定向与失败重试retryAndFollowUpInterceptor
用户的请求头处理,响应处理Cookie持久性策略 BridgeInterceptor
缓存请求、响应缓存的写入 (客户端设置的缓存策略)CacheInterceptor
发送请求,读取服务器的响应CallServerInterceptor
最后通过RealInterceptorChain调用proceed接口启动拦截器。
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);
return chain.proceed(originalRequest);
}
几个管件类和接口
Interceptor.java
public interface Interceptor {
Response intercept(Chain chain) throws IOException;
interface Chain {
Request request();
Response proceed(Request request) throws IOException;
Connection connection();
}
}
RealInterceptorChain.java
public final class RealInterceptorChain implements Interceptor.Chain {
private final List<Interceptor> interceptors;
private final StreamAllocation streamAllocation;
private final HttpCodec httpCodec;
private final Connection connection;
private final int index;
private final Request request;
private int calls;
public RealInterceptorChain(List<Interceptor> interceptors, StreamAllocation streamAllocation,
HttpCodec httpCodec, Connection connection, int index, Request request) {
this.interceptors = interceptors;
this.connection = connection;
this.streamAllocation = streamAllocation;
this.httpCodec = httpCodec;
this.index = index;
this.request = request;
}
...
@Override public Response proceed(Request request) throws IOException {
return proceed(request, streamAllocation, httpCodec, connection);
}
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
Connection connection) throws IOException {
...
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(
interceptors, streamAllocation, httpCodec, connection, index + 1, request);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
...
return response;
}
}
RealInterceptorChain实现了Interceptor接口,调用源头来自于RealCall的getResponseWithInterceptorChain方法
调用流程:
1.加入自定义拦截器。然后将okhttp内置的一些拦截器按照功能顺序add进list里。
2.定义整个拦截器工作的源头。new一个RealInterceptorChain,idnex为0,把初始的request传了进去,然后调用proceed获取请求的response
3.RealInterceptorChain实现了Interceptor.Chain接口,在proceed方法中获取上一个拦截器包装的Request,然后将下一个拦截器和整个Request的信息包装成RealInterceptorChain,并且会调用当前拦截器的intercept方法把下一个拦截器(RealInterceptorChain)传进去,并且获取到Response
4.CallServerInterceptor的Interceptor完成最终的网络请求并返回response

本文深入解析OkHttp的Interceptor机制,从RealCall的getResponseWithInterceptorChain方法开始,介绍如何构建拦截器链。内容涵盖拦截器的调用流程,包括自定义拦截器、重定向与失败重试、请求头处理、缓存策略以及网络请求的执行。同时探讨了Interceptor.java和RealInterceptorChain.java的角色和交互过程。
490

被折叠的 条评论
为什么被折叠?



