I'm trying to cache response via OkHttp and Retrofit. I understand there are several questions similar to mine but none of those are able to address my issue.
Following is my Interceptor responsible for modifying the headers.
private static class CachingControlInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response originalResponse = chain.proceed(request);
return originalResponse.newBuilder()
.header("Cache-Control", (UtilityMethods.isNetworkAvailable()) ?
"public, max-age=60" : "public, max-stale=604800")
.build();
}
}
Now, this works perfectly in the first case -
- Internet connection is available.
- A valid response is received and cached.
- Disconnect the device from the internet.
- Send the same request as previous one within a minute -> Response is same as last
- Next, send the same request after a minute is complete -> No response (
UnknownHostException
)
This makes the first part ("public, max-age=60"
) complete.
But, somehow, "public, max-age=60"
part does not work at all.
This part should enable okhttp to fetch the a week old stale data when the device is offline but instead, I get UnknownHostException
.