Interceptor chặn HTTP request/response để chỉnh sửa chúng. Application interceptor chạy một lần cho mỗi request, còn network interceptor chạy cho mỗi lần gọi network thực tế. Dùng phổ biến: thêm auth header, log request/response, xử lý token refresh, cache.
Ví dụ
OkHttpClient().addInterceptor { chain -> chain.proceed(chain.request().newBuilder().addHeader("Authorization", token).build()) }Interceptors intercept HTTP requests and responses to modify them. Application interceptors run once per request, while network interceptors run for each actual network call. Common uses: adding authentication headers, logging, handling token refresh, caching.
Example
OkHttpClient().addInterceptor { chain -> chain.proceed(chain.request().newBuilder().addHeader("Authorization", token).build()) }