Cache requests using retrofit2 and okhttp3
I am using retrofit2 for cash responses using a cache interceptor
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
if (NetworkUtil.isConnected(context)) {
return originalResponse.newBuilder()
.header("Cache-Control", "public, max-age=" + MAX_AGE)
.build();
} else {
return originalResponse.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" + MAX_STALE)
.build();
}
}
but i need to cache certain requests not all, how to do this?
+3
source to share
1 answer
public Response intercept(Chain chain)
Here the chain contains the request that is being made.
public Response intercept(Chain chain) {
Request request = chain.request()
}
You can check this request and act on the information, for example with request.url()
and request.headers()
.
Read more about interception here .
+1
source to share