title: OkHttp + Retrofit offline cache not working
body: <p>I'm requesting json data from two servers. OkHttp doesn't cache responses from the first server, responses from the second server are caching properly.</p>
  
  <p>Response from the first server, cache is not working for some reason:</p>
  
  <pre><code>&lt;--- 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
  &lt;--- END HTTP (1381-byte body)
  </code></pre>
  
  <p>Response from the first server when device is offline:</p>
  
  <pre><code>---&gt; HTTP GET
  Cache-Control: public, only-if-cached, max-stale=2419200
  ---&gt; END HTTP (no body)
  &lt;--- HTTP 504
  &lt;--- END HTTP (0-byte body)
  </code></pre>
  
  <p>Response from the second server, cache works:</p>
  
  <pre><code>&lt;--- 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
  &lt;--- END HTTP (92663-byte body)
  </code></pre>
  
  <p>Response from the second server when device is offline:</p>
  
  <pre><code>&lt;--- 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 is stale"
  Validating map...
  &lt;--- END HTTP (92663-byte body)
  </code></pre>
  
  <p>I followed <a href="https://stackoverflow.com/questions/23429046/can-retrofit-with-okhttp-use-cache-data-when-offline">this answer</a>, here's my rest adapter:</p>
  
  <pre><code>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;
  }
  </code></pre>