Android advanced light-network programming and network framework,
No1:
Volley source code analysis:
Volley. newRequestQueue->
RequestQueue. start ()->
CacheDispatcher. start ()-> CacheDispatcher. run ()->
NetworkDispatcher. start ()-> NetworkDispatcher. run ()->
RequestQueue. add ()->
BasicNetwork. receivmrequest ()->
HttpStack. receivmrequest ()->
Delivery. postResponse ()->
ExecutorDelivery. ResponseDeliveryRunnable->
Request. deliverResponse ()->
Response. Listener. onResponse ()->
Note:
Volley is divided into three types of threads: main thread, cache scheduling thread, and network scheduling thread. Four network scheduling threads are enabled by default.
First, the request is added to the cache queue, and the cache scheduling thread extracts the request from the cache queue. If the cache response of the request is found, the cached response is directly read and parsed, and then the callback is sent to the main thread;
If no cached response is found, the request is added to the network queue. Then, the network scheduling thread poll the requests in the network queue and sends the HTTP request, parse the response and save the response to the cache, and call back it to the main thread.
No2:
OkHttp source code analysis:
OkHttpClient. newCall (request)->
RealCall. enqueue->
Dispatcher. enqueue->
RealCall.exe cute->
Client. dispatcher (). finished ()-> promotecils->
AsyncCall.exe cute-> getResponseWithInterceptorChain-> getResponse ()->
HttpEngine. sendRequest-> readResponse->
RealCall. getResponse->
HttpEngine. recover->
No3:
The core of Okhttp connection pool reuse is to use Deque <RealConnection> to store connections, and perform operations on Deque through put, get, connectionBecameIdle, and evictAll, in addition, the StreamAllocation object in the connection is determined to automatically recycle the connection.
No4:
Sort fit annotation Classification
1) HTTP Request Method annotation: GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS, HTTP (the first seven types can be replaced or extended)
2) Tag annotation: FormUrlEncoded, Multipart, Streaming (Stream)
3) parameter annotation: Header, Headers, Body, Path, Field, FieldMap, Part, PartMap, Query, QueryMap
No5:
Retrofit source code analysis
Fit. Builder ()->
Platform. get ()-> findPlatform->
Keep fit. build ()->
Using Fit. create-> loadServiceMethod->
ServiceMethod. Builder->
ExecutorCallAdapterFactory. get->
ExecutorCallbackCall. enqueue->
Delegate. enqueue-> OkHttpCall. enqueue->
Okhttp3.Call. enqueue-> parseResponse-> toResponse->
GsonConverterFactory. responseBodyConverter->
GsonResponseBodyConverter. convert->
The Call. enqueue method mainly uses OkHttp to request the network, converts the returned Response data, and calls back the Response to the UI thread.