title: HTTP Caching with Retrofit 2.0.x
body: <p>I'm trying to cache some responses in my app using Retrofit 2.0, but I'm missing something.</p>
  
  <p>I installed a caching file as follows:</p>
  
  <pre><code>private static File httpCacheDir;
  private static Cache cache;
  try {
      httpCacheDir = new File(getApplicationContext().getCacheDir(), "http");
      httpCacheDir.setReadable(true);
      long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
      HttpResponseCache.install(httpCacheDir, httpCacheSize);
      cache = new Cache(httpCacheDir, httpCacheSize);
      Log.i("HTTP Caching", "HTTP response cache installation success");
  } catch (IOException e) {
      Log.i("HTTP Caching", "HTTP response cache installation failed:" + e);
  }
  
  public static Cache getCache() {
          return cache;
      }
  </code></pre>
  
  <p>which creates a file in <code>/data/user/0/&lt;PackageNmae&gt;/cache/http</code>
  , then prepared a network interceptor as follows: </p>
  
  <pre><code>public class CachingControlInterceptor implements Interceptor {
      @Override
      public Response intercept(Chain chain) throws IOException {
          Request request = chain.request();
  
          // Add Cache Control only for GET methods
          if (request.method().equals("GET")) {
              if (ConnectivityUtil.checkConnectivity(getContext())) {
                  // 1 day
                  request.newBuilder()
                      .header("Cache-Control", "only-if-cached")
                      .build();
              } else {
                  // 4 weeks stale
                  request.newBuilder()
                      .header("Cache-Control", "public, max-stale=2419200")
                      .build();
              }
          }
  
          Response originalResponse = chain.proceed(chain.request());
          return originalResponse.newBuilder()
              .header("Cache-Control", "max-age=86400")
              .build();
      }
  }
  </code></pre>
  
  <p>my <code>Retrofit</code> and <code>OkHttpClient</code> instance: </p>
  
  <pre><code>OkHttpClient client = new OkHttpClient();
  client.setCache(getCache());
  client.interceptors().add(new MainInterceptor());
  client.interceptors().add(new LoggingInceptor());
  client.networkInterceptors().add(new CachingControlInterceptor());
  Retrofit restAdapter = new Retrofit.Builder()
          .client(client)
          .baseUrl(Constants.BASE_URL)
          .addConverterFactory(GsonConverterFactory.create(gson))
          .build();
  
  productsService = restAdapter.create(ProductsService.class);
  </code></pre>
  
  <p>where <code>ProductsService.class</code> contains:</p>
  
  <pre><code>@Headers("Cache-Control: max-age=86400")
  @GET("categories/")
  Call&lt;PagedResponse&lt;Category&gt;&gt; listCategories();
  </code></pre>
  
  <p>and </p>
  
  <pre><code>Call&lt;PagedResponse&lt;Category&gt;&gt; call = getRestClient().getProductsService().listCategories();
  call.enqueue(new GenericCallback&lt;PagedResponse&lt;Category&gt;&gt;() {
        // whatever 
        // GenericCallback&lt;T&gt; implements Callback&lt;T&gt;
     }
  });
  </code></pre>
  
  <p>The question here is: How to make it access cached responses when device being offline?</p>
  
  <p>Header of backend response are: </p>
  
  <pre><code>Allow → GET, HEAD, OPTIONS
  Cache-Control → max-age=86400, must-revalidate
  Connection → keep-alive
  Content-Encoding → gzip
  Content-Language → en
  Content-Type → application/json; charset=utf-8
  Date → Thu, 17 Dec 2015 09:42:49 GMT
  Server → nginx
  Transfer-Encoding → chunked
  Vary → Accept-Encoding, Cookie, Accept-Language
  X-Frame-Options → SAMEORIGIN
  x-content-type-options → nosniff
  x-xss-protection → 1; mode=block
  </code></pre>