An example of the HTTP Interceptor interceptor usage in the Android okhttp package _android

Source: Internet
Author: User

The Interceptor of Okhttp (Github:https://github.com/square/okhttp) is like the name "Interceptor", intercepting your Request to do something you want to do and send it out. For example:
1. Automatically plus the user is currently using the language sent out to obtain the corresponding language content.
2. Send the request to the sigunature of the request.
In the okhttp, it is divided into application interceptor and network interceptor two species. Application Interceptor will be able to be cache up. such as the official website of the picture:

Interceptors are a powerful mechanism provided by OKHTTP for the unified processing of HTTP requests and responses. Interceptors are similar to filters in the Servlet specification for implementation and use. Multiple interceptors can be linked together to form a chain. Interceptors are executed sequentially in the order in which they are arranged on the chain. When the interceptor executes, the requested request object can be modified first, then the Response object of the response can be modified and then returned.
The Interceptor interface contains only one method intercept, and its arguments are Chain objects. The Chain object represents the current interceptor chain. You can get to the current request object by using the Chain request method. After the Request object is used, the execution of the interceptor chain is continued through the proceed method of the Chain object. When the execution is complete, additional processing can be performed on the resulting Response object.
Interceptor to log request and response information:

public class Logginginterceptor implements interceptor {public
Response intercept (Chain Chain) throws IOException C2/>request Request = Chain.request ();

 Long T1 = System.nanotime ();
 System.out.println (String.Format ("Send request: [%s]%s%n%s",
   Request.url (), Chain.connection (), request.headers ());

 Response Response = chain.proceed (request);

 Long t2 = System.nanotime ();
 System.out.println (String.Format ("Receive response: [%s]%.1fms%n%s",
   response.request (). URL (), (T2-T1)/1e6d, Response.headers ()));

 return response
}
}

The interceptor in the okhttp is divided into two kinds, application and network interceptor. Application interceptors are invoked only once for each HTTP response, and can be retried by calling the Chain.proceed method without calling the Chain.proceed method. The response of the network interceptor to automatic redirection and retry in the call execution is also invoked, and will not be invoked if the response is from the cache.
adding applications and network interceptors

Client.interceptors (). Add (New Logginginterceptor ()); Add Application Interceptor
client.networkinterceptors (). Add (New Logginginterceptor ());//Add Network Interceptor

To do calculate the Sigunature for the Request, use the following method:

 public class Apiclient {Interceptor Signedrequestinterceptor = new Interceptor () {@
   Override public Response Intercept (Chain Chain) throws IOException {Response = null;
    try {Request originalrequest = Chain.request ();
    Request signedrequest = signrequestutil.signrequest (originalrequest);
   Response = Chain.proceed (signedrequest); catch (NoSuchAlgorithmException |
   InvalidKeyException e) {e.printstacktrace ();
  return response;

 }
 };
 Okhttpclient client = new Okhttpclient ();
Client.networkinterceptors (). Add (Signedrequestinterceptor); public class Signrequestutil {public static request Signrequest (Request OriginalRequest) {Request.builder REQUESTBU

  Ilder = Originalrequest.newbuilder ();
  Do all the things you need to do in this, recreate a Request and send it out.
 return Requestbuilder. Headers (getsignedheaders (originalrequest))-build (); }} 

Related Article

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.