No1: Volley源碼分析: Volley.newRequestQueue-> RequestQueue.start()-> CacheDispatcher.start()->CacheDispatcher.run()-> NetworkDispatcher.start()->NetworkDi ...
No1:
Volley源碼分析:
Volley.newRequestQueue->
RequestQueue.start()->
CacheDispatcher.start()->CacheDispatcher.run()->
NetworkDispatcher.start()->NetworkDispatcher.run()->
RequestQueue.add()->
BasicNetwork.performRequest()->
HttpStack.performRequest()->
Delivery.postResponse()->
ExecutorDelivery.ResponseDeliveryRunnable->
Request.deliverResponse()->
Response.Listener.onResponse()->
說明:
Volley分為三類線程,分別是主線程、緩存調度線程和網路調度線程,其中網路調度線程預設開啟4個。
首先請求會加入緩存隊列,緩存調度線程從緩存隊列中取出請求。如果找到該請求的緩存響應就直接讀取緩存的響應並解析,然後回調給主線程;
如果沒有找到緩存的響應,則將這條請求加入網路隊列,然後網路調度線程會輪詢取出網路隊列中的請求,取出後發送HTTP請求,解析響應並將響應存入緩存,並回調給主線程。
No2:
OkHttp源碼分析:
OkHttpClient.newCall(request)->
RealCall.enqueue->
Dispatcher.enqueue->
RealCall.execute->
client.dispatcher().finished()->promoteCalls->
AsyncCall.execute->getResponseWithInterceptorChain->getResponse()->
HttpEngine.sendRequest->readResponse->
RealCall.getResponse->
HttpEngine.recover->
No3:
Okhttp連接池復用的核心就是用Deque<RealConnection>來存儲連接,通過put、get、connectionBecameIdle和evictAll幾個操作來對Deque進行操作,另外通過判斷連接中的計數對象StreamAllocation來進行自動回收連接。
No4:
Retrofit註解分類
1)HTTP請求方式註解:GET、POST、PUT、DELETE、HEAD、PATCH、OPTIONS、HTTP(可替換或擴展前7種)
2)標記類註解:FormUrlEncoded、Multipart、Streaming(流)
3)參數類註解:Header、Headers、Body、Path、Field、FieldMap、Part、PartMap、Query、QueryMap
No5:
Retrofit源碼解析
Retrofit.Builder()->
Platform.get()->findPlatform->
Retrofit.build()->
Retrofit.create->loadServiceMethod->
ServiceMethod.Builder->
ExecutorCallAdapterFactory.get->
ExecutorCallbackCall.enqueue->
delegate.enqueue->OkHttpCall.enqueue->
okhttp3.Call.enqueue->parseResponse->toResponse->
GsonConverterFactory.responseBodyConverter->
GsonResponseBodyConverter.convert->
Call.enqueue方法主要做的就是用OkHttp來請求網路,將返回的Response進行數據轉換並回調給UI線程