一、網(wǎng)絡(luò)庫選型
目前App開發(fā)網(wǎng)絡(luò)庫技術(shù)選型:

- HttpClient: 在android 5.0就被從源碼中移除了
- HttpUrlConnection: 偏底層,不適合直接使用,封裝起來也比較麻煩。
- Volley:適合數(shù)據(jù)量小但是頻繁的網(wǎng)絡(luò)操作,對大文件下載表現(xiàn)糟糕。
- Okhttp:目前主推的網(wǎng)絡(luò)庫,全面支持各種網(wǎng)絡(luò)請求、文件上傳下載;性能高效,底層線程池提高請求的復(fù)用性;優(yōu)秀的代碼設(shè)計。但是也需要進行二次封裝。
- Retrofit:對Okhttp的二次封裝。
大部分公司要么自己封裝一套Okhttp,要么直接用Retrofit,怎么樣都需要先了解Okhttp。那么就選擇用Okhttp來研究下網(wǎng)絡(luò)庫。
Okhttp項目地址:https://github.com/square/okhttp
二、OkHttp網(wǎng)絡(luò)請求流程
從一個簡單的異步GET請求開始:
File cacheDir = new File(MainActivity.this.getCacheDir(), "okhttpcache");//緩存目錄
Cache mCache = new Cache(cacheDir, 8 * 1024 * 1024); //8M緩存空間
mOkHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS) //連接超時閾值
.writeTimeout(10, TimeUnit.SECONDS) //寫超時閾值
.readTimeout(10, TimeUnit.SECONDS) //讀超時閾值
.retryOnConnectionFailure(true) //當失敗后重試
.cache(mCache)
.build();
String url = "https://www.baidu.com/img/bd_logo1.png";
CacheControl mCacheControl = new CacheControl.Builder()
.noTransform()
.maxAge(6, TimeUnit.SECONDS) //緩存有效期時長
.build();
Request mRequest = new Request.Builder()
.url(url)
.method("GET",null)
.cacheControl(mCacheControl)
.build();
mOkHttpClient.newCall(mRequest).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
if (response.isSuccessful()) {
byte[] bytes = response.body().bytes();
final Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
runOnUiThread(new Runnable() {
@Override
public void run() {
image.setImageBitmap(bmp);
}
});
}
}
});
上面代碼效果是網(wǎng)絡(luò)請求一張圖片加載到ImageView,同時做了緩存。
下面來分析網(wǎng)絡(luò)請求流程:
#OkHttpClient.java
public Builder() {
dispatcher = new Dispatcher();//分發(fā)器
...
connectionPool = new ConnectionPool();//連接池
...
}
先看構(gòu)造方法,從命名看,一個分發(fā)器,一個連接池,mark下。繼續(xù)看newCall方法
@Override public Call newCall(Request request) {
return new RealCall(this, request);//實現(xiàn)類是RealCall
}
看下RealCall,execute是同步請求,enqueue是異步請求,那么重點看下enqueue:
#RealCall.java
void enqueue(Callback responseCallback, boolean forWebSocket) {
synchronized (this) {
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
client.dispatcher().enqueue(new AsyncCall(responseCallback, forWebSocket));
}
這里client.dispatcher()對應(yīng)的正是OkHttpClient中的dispatcher,那么看看他的enqueue方法:
#Dispatcher.java
//ArrayDeque是數(shù)組實現(xiàn)的雙端隊列性能比LinkedList要好。
/** 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<>();
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;
}
從線程池的配置看,就是一個CacheThreadPool。那么它支持高并發(fā)低耗時任務(wù)。
synchronized void enqueue(AsyncCall call) {
//當正在運行的異步請求隊列中的數(shù)量小于64并且正在運行的請求主機數(shù)小于5
if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
runningAsyncCalls.add(call);
executorService().execute(call);
} else {
readyAsyncCalls.add(call);
}
}
當前AsyncCall滿足條件后,異步執(zhí)行隊列添加,并交由線程池執(zhí)行,否則添加到異步準備隊列。執(zhí)行就參考CacheThreadPool:當執(zhí)行execute方法時,向SynchronousQueue提交任務(wù),此時SynchronousQueue需要移除一個任務(wù)去被執(zhí)行,如果此時有空閑線程則交給空閑線程處理,沒有則新建線程處理??臻e線程超過60s沒被使用則回收。
線程池中傳進來的參數(shù)就是AsyncCall它是RealCall的內(nèi)部類,內(nèi)部也實現(xiàn)了execute方法:
#RealCall.java
@Override protected void execute() {
boolean signalledCallback = false;
try {
Response response = getResponseWithInterceptorChain(forWebSocket);
if (canceled) {
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);
}
}
這部分主要是獲取response以及callback回調(diào)。重點關(guān)注getResponseWithInterceptorChain:
private Response getResponseWithInterceptorChain(boolean forWebSocket) throws IOException {
Interceptor.Chain chain = new ApplicationInterceptorChain(0, originalRequest, forWebSocket);
return chain.proceed(originalRequest);
}
創(chuàng)建了ApplicationInterceptorChain,它是一個攔截器鏈。攔截器的作用就是:可以在應(yīng)用拿到response之前,先獲得response,對其中某些數(shù)據(jù)進行監(jiān)控,在有必要的情況下,對response的某些內(nèi)容(比如response的header,body,response內(nèi)的request的header,body)進行更改。
ApplicationInterceptorChain
public Response proceed(Request request) throws IOException {
if (this.index < Call.this.client.interceptors().size()) {
Chain chain = Call.this.new ApplicationInterceptorChain(this.index + 1, request, this.forWebSocket);
Interceptor interceptor = (Interceptor)Call.this.client.interceptors().get(this.index);
Response interceptedResponse = interceptor.intercept(chain);
if (interceptedResponse == null) {
throw new NullPointerException("application interceptor " + interceptor + " returned null");
} else {
return interceptedResponse;
}
} else {
return Call.this.getResponse(request, this.forWebSocket);
}
}
最終都會返回response,那么直接來看看getResponse方法:
#Call.java
Response getResponse(Request request, boolean forWebSocket) throws IOException {
...
// Create the initial HTTP engine. Retries and redirects need new engine for each attempt.
engine = new HttpEngine(client, request, false, false, forWebSocket, null, null, null);
int followUpCount = 0;
while (true) {
if (canceled) {
engine.releaseStreamAllocation();
throw new IOException("Canceled");
}
boolean releaseConnection = true;
try {
engine.sendRequest();
engine.readResponse();
releaseConnection = false;
} catch (RequestException e) {
// The attempt to interpret the request failed. Give up.
throw e.getCause();
} catch (RouteException e) {
// The attempt to connect via a route failed. The request will not have been sent.
HttpEngine retryEngine = engine.recover(e.getLastConnectException(), null);
if (retryEngine != null) {
releaseConnection = false;
engine = retryEngine;
continue;
}
...
}
}
這里網(wǎng)絡(luò)請求和響應(yīng)的核心邏輯封裝在HttpEngine中。兩個關(guān)鍵方法:sendRequest() 和 readResponse(),發(fā)送請求與接收響應(yīng)。另外這里要注意:當發(fā)生IOException或者RouteException時會執(zhí)行HttpEngine的recover方法,該方法實現(xiàn)失敗重連。
Call 的execute同步邏輯就不分析了。
整體流程如下圖所示:

三、Okhttp緩存策略
上節(jié)講到HttpEngine,來看下sendRequest方法,作用是發(fā)送請求,邏輯中包含了緩存策略,所以先來簡單了解下Http緩存相關(guān)知識點。
3.1Http緩存機制
http有強制和對比兩種緩存方式:
-
強制緩存:在有效時間內(nèi)直接獲取本地緩存。對應(yīng)有倆請求頭參數(shù)
Cache-Control
在請求中使用Cache-Control 時,它可選的值有:
在響應(yīng)中使用Cache-Control 時,它可選的值有:

Expires: 過期時間
超過過期時間需要重新請求服務(wù)器
-
對比緩存:先獲取本地緩存數(shù)據(jù)標識,然后請求服務(wù)器確認緩存數(shù)據(jù)是否失效,如果沒失效返回304,則直接使用標識對應(yīng)的本地緩存,如果失效,服務(wù)器會返回新的數(shù)據(jù)再緩存起來。
ETag:緩存數(shù)據(jù)標識
If-None-Match:向服務(wù)器發(fā)請求時加的flag。 例如:If-None-Match:1ec5-502264e2ae4c0
Last-Modified: 由服務(wù)器返回,表示響應(yīng)的數(shù)據(jù)最近修改的時間。
If-Modified-Since:由客戶端請求,表示詢問服務(wù)器這個時間是不是上次修改的時間。如果服務(wù)端該資源的修改時間小于等于If-Modified-Since指定的時間,說明資源沒有改動,返回響應(yīng)狀態(tài)碼304,可以使用緩存。如果服務(wù)端該資源的修改時間大于If-Modified-Since指定的時間,說明資源又有改動了,則返回響應(yīng)狀態(tài)碼200和最新數(shù)據(jù)給客戶端,客戶端使用響應(yīng)返回的最新數(shù)據(jù)。
3.2 Http緩存規(guī)則:

好,那么來看Okttp是怎么做緩存策略的,還是拿開頭的GET請求小例子,我截取了緩存部分的內(nèi)容:
File cacheDir = new File(MainActivity.this.getCacheDir(), "okhttpcache");//緩存目錄
Cache mCache = new Cache(cacheDir, 8 * 1024 * 1024); //8M緩存空間
mOkHttpClient = new OkHttpClient.Builder()
.cache(mCache)
CacheControl mCacheControl = new CacheControl.Builder()
.noTransform()
.maxAge(6, TimeUnit.SECONDS) //緩存有效期時長
.build();
Request mRequest = new Request.Builder()
.cacheControl(mCacheControl)
生成的緩存文件:
/data/data/com.stan.okhttpdemo/cache/okhttpcache # ls -al
total 56
drwx------ 2 u0_a138 u0_a138 4096 2019-11-23 10:50 .
drwxrwx--x 3 u0_a138 u0_a138 4096 2019-11-23 10:50 ..
-rw------- 1 u0_a138 u0_a138 6677 2019-11-23 10:50 5f8dcf06d74b796a51269303a0d2e07b.0
-rw------- 1 u0_a138 u0_a138 7877 2019-11-23 10:50 5f8dcf06d74b796a51269303a0d2e07b.1
-rw------- 1 u0_a138 u0_a138 124 2019-11-23 10:50 journal
cat 5f8dcf06d74b796a51269303a0d2e07b.0
GET
0
HTTP/1.1 200 OK
14
Accept-Ranges: bytes
Cache-Control: max-age=315360000
Connection: Keep-Alive
Content-Length: 7877
Content-Type: image/png
Date: Sat, 23 Nov 2019 02:50:48 GMT
Etag: "1ec5-502264e2ae4c0"
Expires: Tue, 20 Nov 2029 02:50:48 GMT
Last-Modified: Wed, 03 Sep 2014 10:00:27 GMT
好,了解這些之后,咱們再來看HttpEngine的兩個方法。
public final class HttpEngine {
...
public void sendRequest(){//讀緩存or網(wǎng)絡(luò)請求
先查找是否有可用的Cache,然后通過Cache找到請求對應(yīng)的緩存,然后將請求和緩存交給緩存策略去判斷使用請求還是緩存,
得出結(jié)果后,再判斷使用緩存還是請求,如果使用緩存,用緩存構(gòu)造響應(yīng)直接返回,如果使用請求,那么開始網(wǎng)絡(luò)請求流程。
}
...
public void readResponse(){//讀取網(wǎng)絡(luò)響 、存儲緩存
讀取響應(yīng),然后存儲緩存:
如上面文件介紹
通過maybeCache緩存頭部信息到:/data/data/com.stan.okhttpdemo/cache/okhttpcache/ 5f8dcf06d74b796a51269303a0d2e07b.0
通過cacheWritingResponse 緩存響應(yīng)體信息到:/data/data/com.stan.okhttpdemo/cache/okhttpcache/ 5f8dcf06d74b796a51269303a0d2e07b.1
緩存只支持GET請求方式。
最后寫文件保存通過DiskLruCache,按照LRU這種最近最少使用刪除的原則,當總的大小超過限定大小后,刪除最近最少使用的緩存文件。
緩存空間指定Cache mCache = new Cache(cacheDir, 8 * 1024 * 1024); //8M緩存空間
}
緩存具體源碼分析可以參考:http://m.itdecent.cn/p/00d281c226f6
四、Okhttp復(fù)用連接池
通常我們進行HTTP連接網(wǎng)絡(luò)的時候我們會進行TCP的三次握手,然后傳輸數(shù)據(jù),然后再四次揮手釋放連接。但是大量連接每次連接關(guān)閉都要三次握手四次分手很顯然性能低下。因此http有一種叫做keepalive connections的機制,它可以在傳輸數(shù)據(jù)后仍然保持連接,當客戶端需要再次獲取數(shù)據(jù)時,直接使用剛剛空閑下來的連接而不需要再次握手。在HTTP1.1中缺省就是支持keepalive的。

- 非keepalive connections:

- keepalive connections:

在前面OkhttpClient的Builder中我還提到過一個類:ConnectionPool。
public final class ConnectionPool {
//類似CachedThreadPool的線程池,用于執(zhí)行清理空閑連接
private static final Executor executor = new ThreadPoolExecutor(0 /* corePoolSize */,
Integer.MAX_VALUE /* maximumPoolSize */, 60L /* keepAliveTime */, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp ConnectionPool", true));
//最大的空閑socket連接數(shù)
private final int maxIdleConnections;
//socket的keepAlive時間
private final long keepAliveDurationNs;
//Deque維護了一個隊列,RealConnection是socket物理連接
private final Deque<RealConnection> connections = new ArrayDeque<>();
final RouteDatabase routeDatabase = new RouteDatabase();
boolean cleanupRunning;
public ConnectionPool() {
...
//默認空閑的socket最大連接數(shù)為5個,socket的keepAlive時間為5分鐘
this(5, 5, TimeUnit.MINUTES);
}
}
...
}
連接池復(fù)用的核心就是用Deque<RealConnection>來存儲連接,通過put、get、connectionBecameIdle和evictAll幾個操作來對Deque進行操作,另外通過判斷連接中的計數(shù)對象StreamAllocation來進行自動回收連接。
復(fù)用連接池具體源碼分析可以參考:http://m.itdecent.cn/p/ea1587646750
參考:
http://liuwangshu.cn/application/network/8-okhttp3-sourcecode2.html
http://liuwangshu.cn/application/network/7-okhttp3-sourcecode.html
https://blog.csdn.net/qq_29152241/article/details/82011539
http://m.itdecent.cn/p/00d281c226f6
https://blog.csdn.net/u012375924/article/details/82806617
http://m.itdecent.cn/p/ea1587646750
