OkHttp 是一套處理 HTTP 網(wǎng)絡請求的依賴庫寡痰,由 Square 公司設計研發(fā)并開源挪蹭,目前可以在 Java 和 Kotlin 中使用妨退。
對于 Android App 來說,OkHttp 現(xiàn)在幾乎已經(jīng)占據(jù)了所有的網(wǎng)絡請求操作溃列,RetroFit + OkHttp 實現(xiàn)網(wǎng)絡請求似乎成了一種標配。因此它也是每一個 Android 開發(fā)工程師的必備技能膛薛,了解其內(nèi)部實現(xiàn)原理可以更好地進行功能擴展哭廉、封裝以及優(yōu)化。
異步執(zhí)行的隊列
先從OkHttp的基本使用切入:
先是構建請求相叁,通過 Builder() 構建初始化Dispatcher遵绰,Http協(xié)議類型protocols,Cookie壓縮類型增淹,dns等參數(shù)椿访。
請求操作的起點從 OkHttpClient.newCall().enqueue() 方法開始。
- newCall
okHttpClient.newCall 把 request 封裝轉成一個 RealCall
這個方法會返回一個 RealCall 類型的對象虑润,通過它將網(wǎng)絡請求操作添加到請求隊列中成玫。
enqueue方法將Callable對象轉換成一個異步的AsyncCall的runnable對象。
AsyncCall是RealCall的內(nèi)部類,并且把它交給了client的dispatch對象哭当。
Dispatcher 是 OkHttpClient 的調(diào)度器猪腕,是一種門戶模式。主要用來實現(xiàn)執(zhí)行钦勘、取消異步請求操作陋葡。本質(zhì)上是內(nèi)部維護了一個線程池去執(zhí)行異步操作,并且在 Dispatcher 內(nèi)部根據(jù)一定的策略彻采,保證最大并發(fā)個數(shù)腐缤、同一 host 主機允許執(zhí)行請求的線程個數(shù)等。
在enqueue方法會先判斷在運行的asyncCalls數(shù)量是不是已經(jīng)達到最大的64個肛响,并且還會判斷當前運行的主機數(shù)(也就是網(wǎng)頁URL主要部分岭粤,端口前面的主地址)是不是超過了最大的5個。如果都小于特笋,則將當前的AsyncCall加入到正在執(zhí)行的集合中剃浇,否則加入準備執(zhí)行的集合中。加入到正在執(zhí)行的集合中猎物,就會調(diào)用線程池的執(zhí)行方法虎囚。最終去了AsyncCall的execute方法
這里為什么不是run方法的原因是因為AsyncCall繼承的NamedRunnable的run方法中調(diào)用了execute方法
在AsyncCall的execute方法中,就會來到最重要的一個部分霸奕,也就是攔截器的部分。而等攔截器執(zhí)行完吉拳,攔截器方法返回的就是Response质帅。
攔截器
而真正獲取請求結果的方法是在 getResponseWithInterceptorChain 方法中,從名字也能看出其內(nèi)部是一個攔截器的調(diào)用鏈留攒,具體代碼如下:
在添加上述幾個攔截器之前煤惩,會調(diào)用 client.interceptors 將開發(fā)人員設置的攔截器添加到列表當中。
而如果是需要在進行連接后回傳數(shù)據(jù)進行攔截的的話炼邀,也會通過調(diào)用 client.networkInterceptors魄揉。
例如自定義緩存攔截器加載在后面,也就是addNetworkInterceptor拭宁,來實現(xiàn)自定義緩存策略攔截器洛退。如果是調(diào)用 client.interceptors ,則因為回傳數(shù)據(jù)已經(jīng)進過CacheInterceptor杰标,所以無法生效兵怯。而調(diào)用 client.networkInterceptors 則是在接收到resp后,resp會先來到 networkInterceptors 添加的攔截器腔剂,進行緩存策略的更改媒区,再回傳給 CacheInterceptor 上層進行緩存策略判斷。
RetryAndFollowUpInterceptor攔截器
內(nèi)部為一個死循環(huán),每次都會重試丟給下一級處理袜漩。
@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
RealInterceptorChain realChain = (RealInterceptorChain) chain;
Call call = realChain.call();
/**
部分代碼省略
**/
while (true) {
/**
部分代碼省略
**/
Response response;
boolean releaseConnection = true;
try {
response = realChain.proceed(request, streamAllocation, null, null);
releaseConnection = false;
} catch (RouteException e) {
// The attempt to connect via a route failed. The request will not have been sent.
if (!recover(e.getLastConnectException(), streamAllocation, false, request)) {
throw e.getLastConnectException();
}
releaseConnection = false;
continue;
} catch (IOException e) {
// An attempt to communicate with a server failed. The request may have been sent.
boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
if (!recover(e, streamAllocation, requestSendStarted, request)) throw e;
releaseConnection = false;
continue;
} finally {
/**
部分代碼省略
**/
}
if (++followUpCount > MAX_FOLLOW_UPS) {
streamAllocation.release();
throw new ProtocolException("Too many follow-up requests: " + followUpCount);
}
if (followUp.body() instanceof UnrepeatableRequestBody) {
streamAllocation.release();
throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
}
if (!sameConnection(response, followUp.url())) {
streamAllocation.release();
streamAllocation = new StreamAllocation(client.connectionPool(),
createAddress(followUp.url()), call, eventListener, callStackTrace);
this.streamAllocation = streamAllocation;
} else if (streamAllocation.codec() != null) {
throw new IllegalStateException("Closing the body of " + response
+ " didn't close its backing stream. Bad interceptor?");
}
/**
部分代碼省略
**/
}
是否跳出循環(huán)看是否為致命異常绪爸,如果不是致命異常起愈,例如連接超時前域,則進行重試。
并且在這個攔截器中媳瞪,還會處理重定向307粘优、308等仇味。會通過獲取新的頭部信息,生產(chǎn)一個新的請求雹顺,交給下級丹墨。
BridgeInterceptor攔截器
主要設置一些通用的請求頭,Content-type嬉愧,connection贩挣,content-length,Cookie没酣。做一些返回的處理王财,如果被壓縮,采用Zip解壓裕便,保存Cookie绒净。
@Override public Response intercept(Chain chain) throws IOException {
Request userRequest = chain.request();
Request.Builder requestBuilder = userRequest.newBuilder();
RequestBody body = userRequest.body();
if (body != null) {
MediaType contentType = body.contentType();
if (contentType != null) {
requestBuilder.header("Content-Type", contentType.toString());
}
long contentLength = body.contentLength();
if (contentLength != -1) {
requestBuilder.header("Content-Length", Long.toString(contentLength));
requestBuilder.removeHeader("Transfer-Encoding");
} else {
requestBuilder.header("Transfer-Encoding", "chunked");
requestBuilder.removeHeader("Content-Length");
}
}
if (userRequest.header("Host") == null) {
requestBuilder.header("Host", hostHeader(userRequest.url(), false));
}
if (userRequest.header("Connection") == null) {
requestBuilder.header("Connection", "Keep-Alive");
}
// If we add an "Accept-Encoding: gzip" header field we're responsible for also decompressing
// the transfer stream.
boolean transparentGzip = false;
if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
transparentGzip = true;
requestBuilder.header("Accept-Encoding", "gzip");
}
List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
if (!cookies.isEmpty()) {
requestBuilder.header("Cookie", cookieHeader(cookies));
}
if (userRequest.header("User-Agent") == null) {
requestBuilder.header("User-Agent", Version.userAgent());
}
Response networkResponse = chain.proceed(requestBuilder.build());
HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());
Response.Builder responseBuilder = networkResponse.newBuilder()
.request(userRequest);
if (transparentGzip
&& "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
&& HttpHeaders.hasBody(networkResponse)) {
GzipSource responseBody = new GzipSource(networkResponse.body().source());
Headers strippedHeaders = networkResponse.headers().newBuilder()
.removeAll("Content-Encoding")
.removeAll("Content-Length")
.build();
responseBuilder.headers(strippedHeaders);
String contentType = networkResponse.header("Content-Type");
responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));
}
return responseBuilder.build();
}
CacheInterceptor攔截器
負責 HTTP 請求的緩存處理。
CacheInterceptor 主要做以下幾件事情:
- 根據(jù) Request 獲取當前已有緩存的 Response(有可能為 null)偿衰,并根據(jù)獲取到的緩存 Response挂疆,創(chuàng)建 CacheStrategy 對象。
- 通過 CacheStrategy 判斷當前緩存中的 Response 是否有效(比如是否過期)下翎,如果緩存 Response 可用則直接返回缤言,否則調(diào)用 chain.proceed() 繼續(xù)執(zhí)行下一個攔截器,也就是發(fā)送網(wǎng)絡請求從服務器獲取遠端 Response视事。具體如下:
- 如果從服務器端成功獲取 Response胆萧,再判斷是否將此 Response 進行緩存操作。
ConnectInterceptor攔截器
負責建立與服務器地址之間的連接俐东,也就是 TCP 連接跌穗。
建立Socket連接連接緩存,封裝HttpCodec里面封裝了okio的輸入輸出流虏辫,就可以向服務器寫數(shù)據(jù)和返回數(shù)據(jù)瞻离。
CallServerInterceptor攔截器
CallServerInterceptor 是 OkHttp 中最后一個攔截器,也是 OkHttp 中最核心的網(wǎng)路請求部分乒裆,其 intercept 方法如下:
如上圖所示套利,主要分為 2 部分推励。藍線以上的操作是向服務器端發(fā)送請求數(shù)據(jù),藍線以下代表從服務端獲取相應數(shù)據(jù)并構建 Response 對象肉迫。
總結
這節(jié)課主要分析了 OkHttp 的源碼實現(xiàn):
OkHttp 內(nèi)部是一個門戶模式验辞,所有的下發(fā)工作都是通過一個門戶 Dispatcher 來進行分發(fā)。
在網(wǎng)絡請求階段通過責任鏈模式喊衫,鏈式的調(diào)用各個攔截器的 intercept 方法跌造。