Implementation principle of Okhttp interceptor

Source: Internet
Author: User

Today's project encountered the need to parse the data received from the push into a structured data type problem required by the application layer, because of the wide variety of push message types, the number of structured data types that are waiting to be parsed, and some that require a few steps of the parse process, and because of the project's historical reasons, Use Protocal Buffer (the data for push is passed in bytes). In the middle of trying the agent, decoration and other modes, have failed, and eventually use the original inheritance to solve the problem, in the process of solving the problem also learned the next okhttp in the implementation of the Interceptor, here to do a simple record:

    • The interface of the Interceptor is defined as follows:

  

As can be seen, in the interceptor callback, we can get two important parameter request and response, and the interface in the callback, will receive a chain type parameter, this parameter holds the request and response related data.

    • See an example of a simple interceptor interface implementation:

The function of the interceptor instance is to print the log separately before the request is issued and after the response is received. Response response=chain.proceed (Request); It is important that it is the key to string up the interception.

    • Then look at the application of a Web request:

Follow-up, Okhttpclient.builder's Addinterceptor () method, you can see that inside the okhttp is using the list to save all the interceptors that were added.

    • Follow Client.newcall (Request), execute (), and view the serial process of the Interceptor:

As you can see from the method above, the request is constructed as a Realcall type by default, and the Execute () method of the Realcall is further viewed:

This line of code in the red box above does not actually perform a network request, but simply places the request in the request pool, allowing it to wait for the dispatcher to follow. And the real execution body in the blue box above, it encapsulates an interceptor chain (Chain), and calls the Chain proced method, passed to the original request object, here to start the interceptor chain call process:

    • Looking at the proceed method, you can see that it uses loop + recursion, using the function call stack, to concatenate interceptors together:

In the above code, the actual execution of the network request is the code in the last row of green boxes, and when there are multiple interceptors, each interceptor will block in the code place in the blue box at execution time, waiting for the next interceptor call to return.

    • To illustrate this, the following scenarios are modeled with one or 2 interceptors in the Interceptor chain, respectively:

Implementation principle of Okhttp interceptor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.