首先看基本使用
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("").build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
}
});
1. OkHttpClient对象client调用内部newCall方法,传入request,得到一个Call对象:
@Override public Call newCall(Request request) {
return new RealCall(this, request, false /* for web socket */);
}
该方法返回一个RealCall对象
2. call实例调用enqueue方法传入回调:
@Override
public void enqueue(Callback responseCallback) {
synchronized (this) {
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
captureCallStackTrace();
client.dispatcher().enqueue(new AsyncCall(responseCallback));//重点
}
从上面代码中可知,RealCall中的enqueue方法中主要任务是调用OkHttpClient对象中的dispatcher()方法返回一个dispatcher对象并调用该对象的enqueue(AsyncCall call)方法,
synchronized void enqueue(AsyncCall call) {
if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
runningAsyncCalls.add(call);
executorService().execute(call);
} else {
readyAsyncCalls.add(call);
}
}
其中,runningAsyncCalls、readyAsyncCalls为存储类型为AsyncCall的Deque
/** Ready async calls in the order they'll be run. */
private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();
/** Running asynchronous calls. Includes canceled calls that haven't finished yet. */
private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();
/** Running synchronous calls. Includes canceled calls that haven't finished yet. */
private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();
executorService()方法返回值为一个核心线程数为0的线程池
public synchronized ExecutorService executorService() {
if (executorService == null) {
executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp Dispatcher", false));
}
return executorService;
}
执行executorService().execute(call);将AsyncCall(RealCall类中的实现了Runnable接口的内部类)对象加入到线程池中的的HashSet(存储类型为Worker)中,并执行Worker对象中的thread,从而执行AsyncCall对象中的execute方法
3. 执行AsyncCall对象中的execute方法
@Override protected void execute() {
boolean signalledCallback = false;
try {
Response response = getResponseWithInterceptorChain();
//通过retryAndFollowUpInterceptor拦截器中的isCanceled状态来确定是回调onFailure还是onResponse
if (retryAndFollowUpInterceptor.isCanceled()) {
signalledCallback = true;
//responseCallback是开始传入的Callback
responseCallback.onFailure(RealCall.this, new IOException("Canceled"));
} else {
signalledCallback = true;
responseCallback.onResponse(RealCall.this, response);
}
} catch (IOException e) {
if (signalledCallback) {
// Do not signal the callback twice!
Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
} else {
responseCallback.onFailure(RealCall.this, e);
}
} finally {
client.dispatcher().finished(this);
}
}
}
在execute方法中首先通过**getResponseWithInterceptorChain()**方法生成Response 对象
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) {
//配置OkHttpClient中设置的List<Interceptor>
interceptors.addAll(client.networkInterceptors());
}
//向服务器发送请求数据;从服务器读取相应数据
interceptors.add(new CallServerInterceptor(forWebSocket));
Interceptor.Chain chain = new RealInterceptorChain(
interceptors, null, null, null, 0, originalRequest);
return chain.proceed(originalRequest);
}
4. 上面方法最后生成RealInterceptorChain对象并执行proceed方法(根据interceptors 的list顺序依次链式执行)
首先看RealInterceptorChain类构造方法
public RealInterceptorChain(List<Interceptor> interceptors, StreamAllocation streamAllocation,
HttpCodec httpCodec, RealConnection connection, int index, Request request) {
this.interceptors = interceptors;//拦截器集合
this.connection = connection;//连接
this.streamAllocation = streamAllocation;
this.httpCodec = httpCodec;
this.index = index;//拦截器索引
this.request = request;
}
重点看proceed方法
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
RealConnection 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;
}
由上述源码可知,proceed方法中会用本次执行的index加1去生成新的RealInterceptorChain 对象,然后在执行本次拦截器的intercept(Chain chain)方法时将其传递进去,在每个interceptor对象的intercept(Chain chain)方法中中都会调用chain.proceed()方法,从而达到链式调用的目的。
5. 各个拦截器的作用
RetryAndFollowUpInterceptor:负责失败重连以及重定向