When using okhttp 3.2 and using interceptors they are not getting called.
Here is the code.
Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
if (isNetworkAvailable(getApplicationContext())) {
int maxAge = 60; // read from cache for 1 minute
return originalResponse.newBuilder()
.removeHeader("Access-Control-Allow-Origin")
.removeHeader("Vary")
.removeHeader("Age")
.removeHeader("Via")
.removeHeader("C3-Request")
.removeHeader("C3-Domain")
.removeHeader("C3-Date")
.removeHeader("C3-Hostname")
.removeHeader("C3-Cache-Control")
.removeHeader("X-Varnish-back")
.removeHeader("X-Varnish")
.removeHeader("X-Cache")
.removeHeader("X-Cache-Hit")
.removeHeader("X-Varnish-front")
.removeHeader("Connection")
.removeHeader("Accept-Ranges")
.removeHeader("Transfer-Encoding")
.removeHeader("Pragma")
.removeHeader("Cache-Control")
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
} else {
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
return originalResponse.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
}
};
int cacheSize = 1 * 1024 * 1024; //1MiB
File cacheFile = new File(getApplicationContext().getCacheDir(),"http-cache");
Cache responseCache = new Cache(cacheFile, cacheSize);
System.out.println("MyApplication.onCreate() cacheDir:" + getApplicationContext().getCacheDir());
client = new OkHttpClient.Builder()
.connectionPool(new ConnectionPool(2,5, TimeUnit.SECONDS))
.cache(responseCache)
.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.build();
More info. I noticed if my wifi is enabled (I do not have cell connection on my test android device) both the network and application interceptors get called. I would expect the application interceptor to get called if the network is enabled or not.
All I am trying to do is use the cached response if the network/cell connection not available.
Thanks!
Are you completely certain the interceptor isn’t being called? Could you create an executable test case to demonstrate the problem?
I updated the issue.It is getting called when the network connection is enabled. But neither interceptor is called when network is disabled. My expectation is the application interceptor should be called then.
Thanks,-Tony
On Tuesday, May 17, 2016 3:27 PM, Jesse Wilson <[email protected]> wrote:
Are you completely certain the interceptor isn’t being called? Could you create an executable test case to demonstrate the problem?—
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
Can you write an executable test case to demonstrate the problem? OkHttp doesn’t know the network is disabled until it attempts to use it, so it’s surprising that a disabled network could prevent application interceptors from working.
Ok I figured out is going wrong. The interceptor is expecting a response but since there is no network chain.proceed(chain.request()) throws an exception about not being able to resolve the url so my println never gets called and it appears as if the interceptor was never called.
So now knowing that I need to figure out how to resolve it. I have to have a way to set a response so the cache gets called. Some of the answers to doing this appear to be wrong.You must have working test code to cover when this.
Where would I find that?
Thanks.
On Tuesday, May 17, 2016 4:56 PM, Jesse Wilson <[email protected]> wrote:
Can you write an executable test case to demonstrate the problem? OkHttp doesn’t know the network is disabled until it attempts to use it, so it’s surprising that a disabled network could prevent application interceptors from working.—
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
We don't have examples of using interceptors to toggle caching. It should be straightforward, but only works for cached responses.
Thanks for the help.The reason I am trying to toggle caching to work because others were doing the same thing. Are you saying that I should not have to do that?? That would be great. What I did was setup a cache and if that is all I need to do it is not working for me or others who chose to try to toggle it.I do not want to log the cache issue if I have an solid example that show it working. And I mean if I lose all connectivity after I had been getting responses that calls still appear to work because of caching.
Thanks,-Tony
On Tuesday, May 17, 2016 8:27 PM, Jesse Wilson <[email protected]> wrote:
Closed #2561.—
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
I don't know of any examples of using interceptors to toggle caching. But please look around and see what's already built.
Thanks. What I discovered is that okhttp is not writing to the cache directory. I re-pointed the directory to DCIM and I see a journal file and it has some odd text in it lobcore.io.DiskLruCache12011052. So no wonder I am not getting anything when I shut off network access I get no saved responses. I did call the response.body.close().
-Tony
On Tuesday, May 17, 2016 9:59 PM, Jesse Wilson <[email protected]> wrote:
I don't know of any examples of using interceptors to toggle caching. But please look around and see what's already built.—
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
@adanecito Hi, I am getting same issue in my app. I send requests at server and its response is getting stored in cache directory. But when i turn off wifi/internet. It does not send request and interceptor does not get called so thats why it does not load data from cache. Can you please describe how did you solved the issue? Thanks
@nomizodiac I have came across the same problem recently. I used the following code to solve it. ( of cource the codes are not elegance at all... ) Hope it can do some help.
private final Interceptor mCacheControlInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!NetworkUtils.isConnected(mContext)) {
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
}
Response originalResponse = null;
try {
originalResponse = chain.proceed(request);
} catch (Exception e) {
e.printStackTrace();
}
// if offline, create a response manually
if (originalResponse == null) {
originalResponse = new Response.Builder().build();
}
if (NetworkUtils.isConnected(mContext)) {
String cacheControl = request.cacheControl().toString();
return originalResponse.newBuilder()
.header("Cache-Control", cacheControl.equals("no-cache") ? "public, max-age=" + DEFAULT_MAX_CACHE_AGE : cacheControl)
.removeHeader("Pragma")
.build();
} else {
return originalResponse.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=2419200")
.removeHeader("Pragma")
.build();
}
}
};
I've also found that when i'm offline, all networkInterceptors not called, but interceptors called.
So, I wrote this:
.addInterceptor(mCacheControlInterceptor)
.addNetworkInterceptor(mCacheControlInterceptor)
It's strange, but solved my problem...
poor guys, it seems that client interceptor logging cannot intercept the network request's logs.