An okhttp3 interceptor that implements stale-if-error
Cache-Control directive
HTTP protocol (in RFC 5861) defines a Cache-Control extension stale-if-error
that allows a stale cached response to be used if the original server is not reachable. OkHttp library, unfortunately, doesn’t implement this extension, hence, we created this project to implement this as an interceptor.
We use the approach presented in the OkHttp issue 1083 by retrying a failed request with an only-if-cached
Cache-Control directive with max-age
of 4 weeks.
Here is an example of how to use this interceptor:
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(new StaleIfErrorInterceptor());
The default 4 weeks max-age
duration can be changed with constructor parameters:
client.addInterceptor(new StaleIfErrorInterceptor(1, TimeUnit.DAYS));
add to your repositories
repositories {
maven { url "https://jitpack.io" }
}
In your app build.gradle, add: compile "com.github.PeelTechnologies:okhttp-staleiferror-interceptor:1.0.1"