Interview frequency: …
Short answer
An application interceptor is called only once per request from the app regardless of retries or cache; a network interceptor observes the real network traffic over the wire.
Explanation
An application interceptor sits at the top of the chain and is ideal for adding fixed headers such as an Authorization bearer token or measuring the total processing time. A network interceptor sits near the actual wire, and is called with every redirect or new connection attempt, and is never called at all if the request is served from the local cache without a network connection.
Code example
val okHttpClient = OkHttpClient.Builder()
// 1. Application Interceptor: Add an entry key
.addInterceptor { chain ->
val request = chain.request().newBuilder()
.addHeader("Authorization", "Bearer $token")
.build()
chain.proceed(request)
}
// 2. Network Interceptor: Monitor real Headers and transfer speed
.addNetworkInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.HEADERS
})
.build()Common mistakes
- Trying to add headers to bypass the cache in a network interceptor and expecting them to work locally before consulting the wire.