基本用法
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对象流程:
获取OkHttpClient是通过OkHttpClient.Builder来创建。整体采用Builder设计模式
添加超时时间可以通过
-
connetionTimeOut方法来设置连接超时时间 -
readTimeOut方法来设置读取的超时时间 -
writeTimeOut方法来设置写入的超时时间
添加Cache,通过cache方法来设置
也可以设置拦截器,通过addInterceptor来设置拦截器。
创建Request对象
Request基本包含四部分
-
method,说明是通过什么方式来进行网络请求 -
HttpUrl说明请求的Url,包含scheme,host,port,path,还有查询参数。 -
Headers,请求的头部信息 -
RequestBody,请求的实体部分
Request,Headers,RequestBody,HttpUrl采用的都是Builder设计模式。
获取Call对象
获取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。
调用Call的方法
Call中两个请求方法:
-
execute方法进行同步请求 -
enqueue方法进行异步网络请求
execute方法
上一节我们知道返回的Call实际为RealCall对象。
调用的顺序如下:
- 调用
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方法
调用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);
}
}
基本结构:
拦截器的执行顺序:
这里只是简单的说明执行顺序,如何运行的可以查看拦截器的intercept方法。
后面我们会详细讲解各个Interceptor的功能。