android - OkHttp + Retrofit offline cache not working -


i'm requesting json data 2 servers. okhttp doesn't cache responses first server, responses second server caching properly.

response first server, cache not working reason:

<--- http 200 cache-control: public, max-age=2230 content-type: application/json; charset=utf-8 expires: sat, 28 mar 2015 11:34:13 gmt last-modified: sat, 28 mar 2015 10:34:13 gmt vary: * server: microsoft-iis/7.5 x-aspnet-version: 4.0.30319 x-powered-by: asp.net access-control-allow-origin: * date: sat, 28 mar 2015 10:57:01 gmt content-length: 1381 okhttp-selected-protocol: http/1.1 okhttp-sent-millis: 1427540192625 okhttp-received-millis: 1427540192940 <--- end http (1381-byte body) 

response first server when device offline:

---> http cache-control: public, only-if-cached, max-stale=2419200 ---> end http (no body) <--- http 504 <--- end http (0-byte body) 

response second server, cache works:

<--- http 200 content-type: application/json; charset=utf-8 date: sat, 28 mar 2015 11:19:51 gmt server: nginx/1.6.2 x-berry-env: p4 x-berry-version: 2.10.0.96e8b7c x-powered-by: express connection: keep-alive okhttp-selected-protocol: http/1.1 okhttp-sent-millis: 1427541591103 okhttp-received-millis: 1427541591385 <--- end http (92663-byte body) 

response second server when device offline:

<--- http 200 content-type: application/json; charset=utf-8 date: sat, 28 mar 2015 11:19:51 gmt server: nginx/1.6.2 x-berry-env: p4 x-berry-version: 2.10.0.96e8b7c x-powered-by: express connection: keep-alive okhttp-selected-protocol: http/1.1 okhttp-sent-millis: 1427541591103 okhttp-received-millis: 1427541591385 warning: 110 httpurlconnection "response stale" validating map... <--- end http (92663-byte body) 

i followed this answer, here's rest adapter:

protected restadapter getrestadapter(final context context) {     if (restadapter == null) {         gson gson = new gsonbuilder()                 .excludefieldswithoutexposeannotation()                 .create();          int cachesize = 10 * 1024 * 1024;         cache cache = new cache(context.getcachedir(), cachesize);         okhttpclient client = new okhttpclient();         client.setcache(cache);          restadapter = new restadapter.builder()                 .setconverter(new gsonconverter(gson))                 .setendpoint(api_url)                 .setclient(new okclient(client))                 .setrequestinterceptor(new requestinterceptor() {                     @override                     public void intercept(requestfacade request) {                         if (utils.isonline(context)) {                             int maxage = 60;                             request.addheader("cache-control", "public, max-age=" + maxage);                         } else {                             int maxstale = 60 * 60 * 24 * 28;                             request.addheader("cache-control",                                     "public, only-if-cached, max-stale=" + maxstale);                         }                     }                 })                 .build();         restadapter.setloglevel(restadapter.loglevel.full);     }      return restadapter; } 

i've solved problem using network interceptors.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -