Im using Retrofit2 & OKHTTP3 for REST API's in my android application. My requirement is I have to cache the requests to use the application in offline mode. The thing is Im able to cache the requests. But when the user goes online again , data should be fetched freshly from backend, it should not serve the cached response. How can I achieve this. Below is my network Interceptor
Network Interceptor
public class CachingInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (Util.isOnline()) {
request = request.newBuilder()
.header("Cache-Control", "only-if-cached")
.build();
} else {
request = request.newBuilder()
.header("Cache-Control", "public, max-stale=2419200")
.build();
}
Response response= chain.proceed(request);
return response.newBuilder()
.header("Cache-Control", "max-age=86400")
.build();
}
}