一、简介
OkHttp是一个优秀的网络请求框架,它是由Square公司开发的。从Android4.4版本后,Okhttp也被纳入了google源码中,目前比较流行的Retrofit框架,底层也是用OkHttp实现的,OkHttp框架的性能和重要性不言而喻。
二、使用流程
我们先来看看一个Okhttp的网络请求的简单流程:
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(10, TimeUnit.SECONDS)
.build();
Request request = new Request.Builder()
.url(url)
.build();
Call call = client.newCall(request);
//同步请求
Response response = call.execute();
System.out.println(response.body().string());
//异步请求
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("Fail");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println(response.body().string());
}
});
第一步:创建http请求客户端类对象:OkhttpClient;
第二步:创建请求信息报文类对象: Request;
第三步:创建Call对象;
第四部:通过call.execute()或者call.enqueue()进行同步或异步请求。
三、OkHttpClient
从上面的例子我们看到,OkhttpClient的创建采用的是Builder模式来进行创建,所以我们先来看看Client类中builder类的构造方法:
public Builder() {
dispatcher = new Dispatcher();
protocols = DEFAULT_PROTOCOLS;
connectionSpecs = DEFAULT_CONNECTION_SPECS;
proxySelector = ProxySelector.getDefault();
cookieJar = CookieJar.NO_COOKIES;
socketFactory = SocketFactory.getDefault();
hostnameVerifier = OkHostnameVerifier.INSTANCE;
certificatePinner = CertificatePinner.DEFAULT;
proxyAuthenticator = Authenticator.NONE;
authenticator = Authenticator.NONE;
connectionPool = new ConnectionPool();
dns = Dns.SYSTEM;
followSslRedirects = true;
followRedirects = true;
retryOnConnectionFailure = true;
connectTimeout = 10_000;
readTimeout = 10_000;
writeTimeout = 10_000;
}
可以看到Builder类已经为我们做了很多默认初始化的工作,这里主要简单介绍几个比较重要的字段:
dispatcher:请求调度器,内部持有同步和异步请求的队列,决定请求是等待还是执行,后面会详细介绍;
protocols:Http协议版本;
eventListenerFactory :一个Call的状态监听器,注意这个是okhttp新添加的功能,目前还不是最终版,在后面的版本中会发生改变的。
proxySelector :代理选择器;
connectionPool :连接池;后面会详细介绍;
通过Builder类完成Client一些默认的配置,在完成配置后,我们调用Builder类的方法完成Client的创建:
public OkHttpClient build() {
return new OkHttpClient(this);
}
private OkHttpClient(Builder builder) {
this.dispatcher = builder.dispatcher;
this.proxy = builder.proxy;
this.protocols = builder.protocols;
this.connectionSpecs = builder.connectionSpecs;
this.interceptors = Util.immutableList(builder.interceptors);
this.networkInterceptors = Util.immutableList(builder.networkInterceptors);
this.proxySelector = builder.proxySelector;
this.cookieJar = builder.cookieJar;
this.cache = builder.cache;
this.internalCache = builder.internalCache;
this.socketFactory = builder.socketFactory;
...
}
四、Request
Okhttp的Request类也采用了Builder模式,我们来看看Request类中Builder的构造方法:
public Builder() {
this.method = "GET";
this.headers = new Headers.Builder();
}
Request中Builder类的构造方法比较简单,设置了默认的请求方式为GET请求,然后创建了headers来保存请求头的信息(也是Builder模式)。
//Builder类的build()方法
public Request build() {
if (url == null) throw new IllegalStateException("url == null");
return new Request(this);
}
private Request(Builder builder) {
this.url = builder.url;
this.method = builder.method;
this.headers = builder.headers.build();
this.body = builder.body;
this.tag = builder.tag != null ? builder.tag : this;
}
五、Call
在创建call对象时,是通过OkHttpClient的newCall(request)方法来创建的,传入一个Request对象:
public Call newCall(Request request) {
return new RealCall(this, request, false /* for web socket */);
}
可以看到newCall方法中,返回了一个RealCall对象,它是Call接口的一个实现类,构造方法传入了当前的Client对象和Request对象。
RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
final EventListener.Factory eventListenerFactory = client.eventListenerFactory();
this.client = client;
this.originalRequest = originalRequest;
this.forWebSocket = forWebSocket;
this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client, forWebSocket);
// TODO(jwilson): this is unsafe publication and not threadsafe.
this.eventListener = eventListenerFactory.create(this);
}
六、Dispatcher
在OkHttpClient类中,我们提到了一个比较重要的类,调度器Dispatcher,他的作用就是将请求事件进行分类调度。我们先来看看这个类中的一些重要的字段;
public final class Dispatcher {
private int maxRequests = 64; //最大请求数
private int maxRequestsPerHost = 5; //当前网络请求host最大请求数
//线程池对象,管理网络请求线程
private ExecutorService executorService;
//异步请求等待队列
private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();
//异步请求正在执行的队列
private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();
//同步请求队列
private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();
...
}
dispatcher中有几个比较重要的变量,一个是线程池,他管理了所有异步请求来进行高效的网络请求操作;另外的就是三个队列,分别是同步请求队列,和异步请求等待对已经异步请求执行队列。
七、同步&异步请求
网络请求有两种方式,一种是同步请求,一种是异步请求,同步请求是通过call的execute方法完成的,而上面Call请求实际上是一个RealCall对象,所以我们来看RealCall中execute方法的实现。
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);
}
}
execute首先同步代码快判断该Call对象是否已经被执行过了,防止多线程执行该方法,如果被执行过则抛出异常,否则设置标志位为true,这里可以看到每一个RealCall对象只能被执行一次。接下来调用Client的调度器dispatcher的executed方法。
synchronized void executed(RealCall call) {
runningSyncCalls.add(call);
}
我们看到,dispatcher中的executed方法只是把RealCall加入到同步请求队列中,回到刚刚的RealCall方法中,看到 Response result = getResponseWithInterceptorChain();这行其实是调用拦截链真正去进行网络请求并获得响应结果(关于拦截链具体后面介绍)。最后,不管网络请求成功与否,都会在finally中调用dispatcher的finished将Call对象从同步请求队列中移除出去。
接下来我们看异步请求,异步请求调用的是RealCall的enqueue方法:
public void enqueue(Callback responseCallback) {
enqueue(responseCallback, false); //继续调用下一层
}
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开始也和同步请求一样做了同步代码进行判断是否已经执行该Call,接下则是先将RealCall封装成AsyncCall对象,AsyncCall对象是一个Runable对象,后面会说到,然后将AsyncCall对象传给dispatcher的enqueue中执行异步请求。我们先来看看dispatcher的enqueue方法:
synchronized void enqueue(AsyncCall call) {
if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
runningAsyncCalls.add(call);
executorService().execute(call);
} else {
readyAsyncCalls.add(call);
}
}
在dispatcher的enqueue方法中,首先判断当前正在进行异步请求的个数是否小于最大请求数maxRequests,并且当前host下的任务数小于最大值,当满足这两个条件时,把该请求加入到正在执行的异步请求队列中,并且把AsyncCall放到线程池中执行,否则加入到等待请求队列中。
我们回头看AsyncCall这个类,AsyncCall是RealCall的内部类,实现了NamedRunnable接口,而实际上NamedRunnable就继承自Runable接口,那么我们就直接看AsyncCall的execute方法:
protected void execute() {
boolean signalledCallback = false;
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) {
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);
}
}
这段代码主要是通过拦截链发送请求获取网络响应,然后根据响应结果回调我们实现的callback接口,最后dispatcher的finished方法移除队列,将一个等待队列请求加到执行队列中去执行。
八、总结
今天主要分析了OkhttpClient和Request以及RealCall这几个类的创建流程,同步和异步请求的流程以及dispatcher调度器在请求中扮演的角色。回头看无论是同步请求还是异步请求,正在获取Response响应都是通过getResponseWithInterceptorChain这个方法来实现的,在下节中会分析这个方法的实现。