基本用法
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
//1. 获取OkHttpClient对象
OkHttpClient client = new OkHttpClient.Builder().build();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
//2. 获取Request对象
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
//3. 获取Call对象,并调用同步的execute方法,或者异步的enqueue方法
Response response = client.newCall(request).execute();
return response.body().string();
}
- 获取
OkHttpClient
对象 - 获取
Request
对象 - 根据
Request
来获取Call
对象 - 最后是调用
Call
的同步方法execute
或者异步的enqueue
方法来进行网络请求。
我们会根据这个四个步骤来分析OkHttp
的源码。
获取OkHttpClient对象
获取OkHttpClient对象流程:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LCDw9Tbj-1615800271979)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/%E8%8E%B7%E5%8F%96OkHttpClient.png?raw=true)]
获取OkHttpClient
是通过OkHttpClient.Builder
来创建。整体采用Builder
设计模式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AsrbFNcr-1615800271981)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/OkHttpClient%E4%B8%8EBuilder.png?raw=true)]
添加超时时间可以通过
connetionTimeOut
方法来设置连接超时时间readTimeOut
方法来设置读取的超时时间writeTimeOut
方法来设置写入的超时时间
添加Cache
,通过cache
方法来设置
也可以设置拦截器,通过addInterceptor
来设置拦截器。
创建Request对象
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E3Nv4NDt-1615800271983)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Request%E7%B1%BB%E7%BB%93%E6%9E%84.png?raw=true)]
Request
基本包含四部分
method
,说明是通过什么方式来进行网络请求HttpUrl
说明请求的Url,包含scheme
,host
,port
,path
,还有查询参数。Headers
,请求的头部信息RequestBody
,请求的实体部分
Request
,Headers
,RequestBody
,HttpUrl
采用的都是Builder
设计模式。
获取Call对象
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FtuOavRE-1615800271984)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/%E8%8E%B7%E5%8F%96Call%E5%AF%B9%E8%B1%A1.png?raw=true)]
获取Call
对象是通过OKHttpClient
的newCall
方法。
public class OkHttpClient implements Cloneable, Call.Factory, WebSocket.Factory {
@Override public Call newCall(Request request) {
return RealCall.newRealCall(this, request, false /* for web socket */);
}
}
然后调用RealCall
的newRealCall
创建Call
对象。
final class RealCall implements Call {
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;
}
}
整体采用抽象工厂模式,如下图OkHttpClient
继承Call
的Factory
,依赖RealCall
。RealCall
继承自Call
。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SdNEirhN-1615800271986)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Call%E7%B1%BB%E7%9B%B8%E5%85%B3%E7%9A%84%E7%BB%93%E6%9E%84.png?raw=true)]
调用Call
的方法
Call
中两个请求方法:
execute
方法进行同步请求enqueue
方法进行异步网络请求
execute方法
上一节我们知道返回的Call
实际为RealCall
对象。
调用的顺序如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YH05RQRf-1615800271987)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Call%E7%9A%84execute%E6%96%B9%E6%B3%95%E6%89%A7%E8%A1%8C%E6%B5%81%E7%A8%8B.png?raw=true)]
- 调用
Dispatcher
的executed
方法 - 调用
RealCall
的getResponseWithInterceptorChain
方法获取Response
(拦截器一节会说明此方法) - 调用
Dispatcher
的finished
方法
final class RealCall implements Call {
@Override public Response execute() throws IOException {
//设置executed为true,说明RealCall已经执行,每个RealCall只能执行一次
synchronized (this) {
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
······
try {
//调用Dispatcher的executed
client.dispatcher().executed(this);
//调用getResponseWithInterceptorChain获取Response
Response result = getResponseWithInterceptorChain();
if (result == null) throw new IOException("Canceled");
return result;
} catch (IOException e) {
throw e;
} finally {
client.dispatcher().finished(this);
}
}
}
enqueue方法
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qWmvJ4Uv-1615800271988)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Call%E7%9A%84enqueue%E6%96%B9%E6%B3%95%E6%89%A7%E8%A1%8C.png?raw=true)]
调用RealCall
的enqueue
的方法的时候会调用到Dispatcher
的enqueue
方法,
final class RealCall implements Call {
@Override
public void enqueue(Callback responseCallback) {
synchronized (this) {
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
//调用到Dispatcher的enqueue
client.dispatcher().enqueue(new AsyncCall(responseCallback));
}
}
Dispatcher
的enqueue
方法中会添加Call
到readyAsyncCalls
中,然后调用其promoteAndExecute
方法。
public final class Dispatcher {
......
void enqueue(AsyncCall call) {
synchronized (this) {
readyAsyncCalls.add(call);
}
promoteAndExecute();
}
......
}
Dispatcher
的promoteAndExecute
方法中会检测当前的运行的Call
时候超过了最大值,超过了直接break
。没有超过会放入本地的executableCalls
的List
集合中,然后会遍历调用executableCalls
中的AsyncCall
对象的executeOn
方法。Dispatcher
的executorService
方法返回的是当前设置的线程池,如果没有设置就使用默认的线程池。
public final class Dispatcher {
......
private boolean promoteAndExecute() {
......
List<AsyncCall> executableCalls = new ArrayList<>();
boolean isRunning;
synchronized (this) {
for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {
AsyncCall asyncCall = i.next();
if (runningAsyncCalls.size() >= maxRequests) break; // Max capacity.
if (runningCallsForHost(asyncCall) >= maxRequestsPerHost) continue; // Host max capacity.
i.remove();
executableCalls.add(asyncCall);
runningAsyncCalls.add(asyncCall);
}
isRunning = runningCallsCount() > 0;
}
for (int i = 0, size = executableCalls.size(); i < size; i++) {
AsyncCall asyncCall = executableCalls.get(i);
asyncCall.executeOn(executorService());
}
return isRunning;
}
......
}
我们接下来分析一下AsyncCall
的executeOn
方法。通过ExecutorService
对象来执行AsyncCall
,AsyncCall
继承自NamedRunnable
,并实现其execute
方法。
final class AsyncCall extends NamedRunnable {
void executeOn(ExecutorService executorService) {
assert (!Thread.holdsLock(client.dispatcher()));
boolean success = false;
try {
executorService.execute(this);
success = true;
} catch (RejectedExecutionException e) {
InterruptedIOException ioException = new InterruptedIOException("executor rejected");
ioException.initCause(e);
eventListener.callFailed(RealCall.this, ioException);
responseCallback.onFailure(RealCall.this, ioException);
} finally {
if (!success) {
client.dispatcher().finished(this); // This call is no longer running!
}
}
}
}
AsyncCall
调用RealCall
的getResponseWithInterceptorChain
方法(拦截器一节会说明此方法)来获取Response
,然后根据是否取消调用相应的回调方法。
final class AsyncCall extends NamedRunnable {
@Override protected void execute() {
······
try {
Response response = getResponseWithInterceptorChain();
if (retryAndFollowUpInterceptor.isCanceled()) {
signalledCallback = true;
responseCallback.onFailure(RealCall.this, new IOException("Canceled"));
} else {
signalledCallback = true;
responseCallback.onResponse(RealCall.this, response);
}
} catch (IOException e) {
······
} finally {
client.dispatcher().finished(this);
}
}
}
拦截器
前面调用RealCall
的getResponseWithInterceptorChain
来获取到响应Response
。其实使用了责任链的方式,我们来一起分析一下。
把拦截器添加到责任链中。
final class RealCall implements Call {
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, this, eventListener, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
return chain.proceed(originalRequest);
}
}
基本结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pH1x6iy2-1615800271989)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Interceptor%E7%B1%BB%E7%BB%93%E6%9E%84.png?raw=true)]
拦截器的执行顺序:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5Uttcwtb-1615800271990)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Interceptor%E7%9A%84%E6%89%A7%E8%A1%8C%E6%B5%81%E7%A8%8B.png?raw=true)]
这里只是简单的说明执行顺序,如何运行的可以查看拦截器的intercept方法。
后面我们会详细讲解各个Interceptor的功能。