Retrofit 43,944 sends every request through OkHttp 47,077 (github.com/square/okhttp (https://github.com/square/okhttp 47,077 ), Apache 2.0, 5.5.0), which pools connections and handles HTTP/2, gzip, redirects and caching. An interceptor receives the request, may change it, calls chain.proceed(), and may inspect the response, like Express 24,430 middleware (MERN Stack Development):
fun provideOkHttp(): OkHttpClient = OkHttpClient.Builder()
.addInterceptor { chain -> // 5.27.2 a header on every call
chain.proceed(chain.request().newBuilder()
.header("X-BookNest-Client", "android/0.1.0")
.build())
}
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.build()HttpLoggingInterceptor logs each exchange under the tag OkHttp; the last line is the test server's log:
I/OkHttp : --> GET http://10.0.2.2:8616/books I/OkHttp : <-- 200 OK http://10.0.2.2:8616/books (609ms, 1899-byte body) 06:24:24 GET /books client=android/0.1.0
addInterceptor() registers an application interceptor, run once per call; addNetworkInterceptor() runs per network request and sees the headers actually sent. Typical uses are auth tokens (refreshed by an Authenticator on 401), common headers and metrics. Use BODY logging only in debug builds, since it prints tokens and personal data, and share one OkHttpClient, which owns thread and connection pools.