Okhttp-1 Framework Process Analysis

OkHttpClient : (Step 1) The object of the client, which represents the client class of all HTTP requests. When we execute it, we will only create it once, and then save it as a global instance, so that we can call it elsewhere in OkHttp When , only this singleton object is used.
Request : (Second step) Create a Request object, which encapsulates some request message information, request URL address, request method (get\post, etc.), various request headers, and builds a mode chain inside it through build to generate our Request object.
RealCall : Get a Call object by calling the newCall method of the Request object. The implementation class of this Call is RealCall, which represents an actual Http request. It is a bridge connecting Request and Response. With this Call, the following asynchronous and synchronous operations can be performed. When RealCall executes the task, it will add its request to the unit in the Dispatcher.   
Dispatcher : Dispatcher class, a thread pool is maintained inside this class, this thread pool is to maintain network requests, including our synchronization and asynchronous. There are three queues in the Dispatcher to maintain our synchronous and asynchronous requests.
interceptors : Interceptors for real server data acquisition. It builds a chain of interceptors, and returns the data obtained by our server by executing each interceptor in the chain in turn. A total of 5 interceptors.
RetryAndFollow : The first interceptor, mainly responsible for two parts of logic, 1) After the network request fails, we will retry. 2) The server returns the current request, and when it needs to be redirected, we will directly initiate the request.
Bridge: The second interceptor, this bridging interceptor, is mainly responsible for setting the content request length, encoding, and compression. It can also add cookies and set other headers for it, mainly for some pre-request operations.
Cache : The third interceptor is responsible for cache management. When the network request has a cache that meets the requirements, we can directly return the Cache to our client. Just use it directly.
Connect : The fourth interceptor finds a suitable connection for the current request. It should be noted here that we may reuse another connection. If this connection can be reused, we don't need to recreate it. , this part of the interceptor also involves the concept of connection pooling.
CallServer : The fifth interceptor, responsible for initiating a real network request to the server, and then receiving the server's return to us to read and return, mainly for network connection.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325907313&siteId=291194637
Recommended