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