These can be added to OkHttpClient using addNetworkInterceptor(). Network Interceptors: These are interceptors that are added between the OkHttp Core Library and the Server. These are the ones that we add using addInterceptor(). We have two types of interceptors as follows:Īpplication Interceptors: These are interceptors that are added between the Application Code(our written code) and the OkHttp Core Library. Another use case can be caching the response of network calls to build the offline-first app, we will learn it later in this blog in detail. Generally, we need to add the logger for each network call, but by using the interceptor, we can add one logger centrally and that will work for all the network calls. We can use the interceptors to do so many things, for example, centrally monitor API calls. They check our boarding pass, put a stamp on it, and then allow us to pass. In simple words, Interceptors are like the security personnel in the security check process at the Airport. So basically, when we do some API call, we can monitor the call or perform some tasks. How to add Interceptors in OkHttpClient?Īccording to documentation, Interceptors are a powerful mechanism that can monitor, rewrite, and retry the API call.Today, we will cover the following topics to master it: In Android, we have many use cases that can be done using the OkHttp Interceptors. We will also see the real use cases where we can use it and how we can use it to get the most out of it. In this blog, we will learn how to work with the OkHttp Interceptors. I am Amit Shekhar, a mentor helping developers in getting high-paying tech jobs.īefore we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.
0 Comments
Leave a Reply. |