0
0
Android Kotlinmobile~5 mins

OkHttp interceptors in Android Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an OkHttp interceptor?
An OkHttp interceptor is a component that can observe, modify, and retry HTTP requests and responses in an OkHttp client.
Click to reveal answer
beginner
Name the two main types of OkHttp interceptors.
The two main types are Application Interceptors and Network Interceptors. Application interceptors work before the request is sent and after the response is received. Network interceptors work at the network level, just before the request is sent to the network and just after the response is received from the network.
Click to reveal answer
beginner
How do you add an interceptor to an OkHttpClient in Kotlin?
You add an interceptor by calling addInterceptor() or addNetworkInterceptor() on the OkHttpClient.Builder before building the client.
Click to reveal answer
beginner
What is a common use case for an OkHttp interceptor?
Common uses include adding headers like authentication tokens, logging request and response data, retrying requests, and modifying requests or responses.
Click to reveal answer
intermediate
What is the difference between proceed(request) and modifying the request inside an interceptor?
proceed(request) sends the request to the next interceptor or network. You can modify the request before calling proceed() to change headers or body.
Click to reveal answer
Which method do you override to create a custom OkHttp interceptor?
Aintercept(chain: Interceptor.Chain)
BonRequest()
ChandleRequest()
Dexecute()
Where does a network interceptor operate in the request lifecycle?
AOnly on cached responses
BBefore the request is sent to the network
CAfter the response is received from the network
DOnly on failed requests
Which OkHttpClient builder method adds an application interceptor?
AaddInterceptor()
BaddNetworkInterceptor()
CsetInterceptor()
DuseInterceptor()
What happens if you don’t call proceed(request) inside an interceptor?
AThe request is retried
BThe request is sent anyway
CThe interceptor throws an exception automatically
DThe request chain stops and no network call is made
Which of these is NOT a typical use of an OkHttp interceptor?
AAdding authentication headers
BLogging request and response data
CChanging the app theme colors
DRetrying failed requests
Explain how OkHttp interceptors can help add authentication tokens to every request.
Think about how you can change the request before it goes out.
You got /3 concepts.
    Describe the difference between application interceptors and network interceptors in OkHttp.
    Consider when each interceptor sees the request and response.
    You got /3 concepts.