OkHttp Interceptors in Android
May 25 2021 · Kotlin 1.4, Android 5, Android Studio 4.1
Part 1: Implementing OkHttp Interceptors
01. Learn about OkHttp Interceptors
Notes: 01. Learn about OkHttp Interceptors
Prerequisites for this course are Android Studio 4, Kotlin 1.4, and either a physical device that can run Android 5 (API 21) or above or an emulator with the same support.
Prior experience with performing network operations using Retrofit and OkHttp is also required since this course focuses on using OkHttp Interceptors.
OkHttp Interceptors are a mechanism that lets you monitor and re-write network calls. If you take a look at the diagram, the app makes an API call that first passes through an interceptor. The interceptor changes the API call and the modified API call is sent to the remote server.
It is possible to chain different interceptors to combine their effects. The order in the chain matter as you will see later in the course. OkHttp maintains a list of Interceptors and hence processes them in-order.
There are two kinds of Interceptors: Application Interceptor and Network Interceptor.
Application interceptors sit between the OkHttp core and the application. They are useful when you are not concerned about the intermediate network responses and want to focus on the final response sent to the app.
Network interceptors sit between the OkHttp core and the network. They are useful when you are concerned about the intermediate network responses and want to focus on things like redirects. Network Interceptors are useful if you want to observe everything that happens between the application making the network call and receiving a final response.