3

I am using retrofit 1.8.0, okhttp 2.1.0 and okhttp-urlconnection 2.1.0, I want to cache the responses I get from server and use them when there is no internet connection, this is my code:

RequestInterceptor requestInterceptor = new RequestInterceptor() {

    @Override
    public void intercept(RequestFacade request) {

        request.addHeader("Accept", "application/json");
        if (Utils.isNetworkAvailable(HancoApplication.this)) {

            int maxAge = 60; // read from cache for 1 minute
            request.addHeader("Cache-Control", "public, max-age=" + maxAge);
        } else {

            int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
            request.addHeader("Cache-Control","public, only-if-cached, max-stale=" + maxStale);
        }
    }
};
OkHttpClient okHttpClient = new OkHttpClient();
File cacheDir = new File(getCacheDir(), "responseCache");
Cache cache = null;
try {

    cache = new Cache(cacheDir, 1024 * 1024 * 12);
    okHttpClient.setCache(cache);
} catch (IOException e) {

    e.printStackTrace();
}
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Const.API_URL).setClient(new OkClient(okHttpClient)).setRequestInterceptor(requestInterceptor).build();
mWebApi = restAdapter.create(WebApi.class);

when I excute the same request without internet on the phone this is what it returns : HTTP/1.1 504 Unsatisfiable Request (only-if-cached) OkHttp-Response-Source: NONE

I can't figure out whats wrong with my code, all the resources on the internet uses the same code!

Luciano Rodríguez
  • 2,239
  • 3
  • 19
  • 32
khaled sultan
  • 75
  • 1
  • 4
  • What cache headers does your HTTP server return? – Jesse Wilson Nov 27 '14 at 00:12
  • Those are all the headers I am getting from the server Cache-Control: "max-age=604800, public, must-revalidate" Connection: keep-alive Content-Type: application/json Date: Thu, 27 Nov 2014 08:14:46 GMT OkHttp-Received-Millis: 1417076086482 OkHttp-Response-Source: NETWORK 200 OkHttp-Selected-Protocol: http/1.1 OkHttp-Sent-Millis: 1417076085965 Server: Cowboy Transfer-Encoding: chunked Via: 1.1 vegur X-Apiary-Ratelimit-Limit: 120 X-Apiary-Ratelimit-Remaining: 119 X-Apiary-Transaction-Id: 5476dd764c3ce90200a3be16 – khaled sultan Nov 27 '14 at 08:17
  • Your response says `must-revalidate` (so the server requires you call it) and your request says `only-if-cached` (so the client refuses to call the server). These are mutually incompatible. – Jesse Wilson Dec 04 '14 at 06:56
  • (OkHttp has a feature request to disregard the server's headers; that'll be coming in a future release). – Jesse Wilson Dec 04 '14 at 06:57
  • I changed the response from server to match your example in [link](https://gist.github.com/swankjesse/5889518) , the server now returns : Cache-Control: public, max-age=60, when I turn the wifi off the do the request again, an UnknownHostException is thrown. what I am doing wrong here ? – khaled sultan Dec 17 '14 at 08:24
  • How abount this @khaledsultan? – Crossle Song Dec 31 '14 at 05:35
  • Simlar problem: http://stackoverflow.com/questions/31321963/how-retrofit-with-okhttp-use-cache-data-when-offline/31606496#31606496 – Gelldur Jul 24 '15 at 09:10

0 Answers0