android网络请求流程图,Android OKHttp系列1-流程总结
1、 調(diào)用示例
同步方式:
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://www.baidu.com").build();
Response response = client.newCall(request).execute();
Log.d(TAG, "response sync:" + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
通過追溯源碼,流程圖如下:
image
異步方式:
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://www.baidu.com").build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
Log.d(TAG, "response async:" + response.toString());
}
});
} catch (Exception e) {
e.printStackTrace();
}
通過追溯源碼,流程圖如下:
image
分析
同步和異步請(qǐng)求的核心方法都是getResponseWithInterceptorChain(),需要注意的是,同步方法沒有在工作線程干活,而異步方法是在線程池里面執(zhí)行,Android不允許在主線程里面做網(wǎng)絡(luò)請(qǐng)求操作,如果同步請(qǐng)求的話,還必須在非主線程中。
異步方式請(qǐng)求是執(zhí)行enqueue方法,有兩個(gè)列表維護(hù)執(zhí)行狀態(tài),runningAsyncCalls和readyAsyncCalls,分別是正在執(zhí)行和等待執(zhí)行列表,而同步方式則是直接提交請(qǐng)求。異步請(qǐng)求當(dāng)一次AsyncCall執(zhí)行完畢之后,在Dispatcher的promoteCalls方法會(huì)做兩個(gè)狀態(tài)列表的切換,等待列表切換到正在執(zhí)行列表,同時(shí)刪除等待列表中最前面的Call。如下:
private void promoteCalls() {
if (runningAsyncCalls.size() >= maxRequests) return; // Already running max capacity.
if (readyAsyncCalls.isEmpty()) return; // No ready calls to promote.
for (Iterator i = readyAsyncCalls.iterator(); i.hasNext(); ) {
AsyncCall call = i.next();
if (runningCallsForHost(call) < maxRequestsPerHost) {
i.remove();
runningAsyncCalls.add(call);
executorService().execute(call);
}
if (runningAsyncCalls.size() >= maxRequests) return; // Reached max capacity.
}
}
遍歷readyAsyncCalls列表里面的call,如果正在運(yùn)行并小于最大的請(qǐng)求數(shù),就可以加到runningAsyncCalls中了,然后接下來execute執(zhí)行這個(gè)請(qǐng)求。
而promoteCalls方法是被Dispatcher的finished方法執(zhí)行。RealCall中同步或異步execute方法執(zhí)行完畢后會(huì)在finally中執(zhí)行client.dispatcher().finished(this)方法,如下:
void finished(AsyncCall call) {
finished(runningAsyncCalls, call, true);
}
/** Used by {@code Call#execute} to signal completion. */
void finished(RealCall call) {
finished(runningSyncCalls, call, false);
}
private void finished(Deque calls, T call, boolean promoteCalls) {
int runningCallsCount;
Runnable idleCallback;
synchronized (this) {
if (!calls.remove(call)) throw new AssertionError("Call wasn't in-flight!");
if (promoteCalls) promoteCalls();
runningCallsCount = runningCallsCount();
idleCallback = this.idleCallback;
}
if (runningCallsCount == 0 && idleCallback != null) {
idleCallback.run();
}
}
第一個(gè)是finished異步請(qǐng)求完成之后調(diào)用,第二finished是同步請(qǐng)求完成之后調(diào)用,最終都會(huì)調(diào)用到帶泛型參數(shù)的finished,并將執(zhí)行完的call從runningAsyncCalls(異步)或runningSyncCalls(同步)中刪除。promoteCalls參數(shù)用來區(qū)分是否是異步請(qǐng)求,如果是的話,執(zhí)行promoteCalls方法。
文章將同步至微信公眾號(hào):Android部落格
總結(jié)
以上是生活随笔為你收集整理的android网络请求流程图,Android OKHttp系列1-流程总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android gps定位工具类,工具类
- 下一篇: font HTML语言,HTML fon