title: Problems with Retrofit Caching
body: <p>I'm having some trouble with caching with Retrofit. It seems like when i'm offline, it does not even look at the cache. Here is the example:</p>
  
  <hr>
  
  <p>This is how i get my CacheReady Retrofit</p>
  
  <pre><code>public class RetrofitCached {
      private static final String BASE_URL = "http://www.omdbapi.com";
      private static final String TAG=RetrofitCached.class.getSimpleName();
  
      public static boolean isNetworkAvailable(Context context) {
  
          ConnectivityManager cm =
                  (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
          return activeNetwork != null &amp;&amp;
                  activeNetwork.isConnectedOrConnecting();
      }
  
      public static Retrofit getCacheEnabledRetrofit(final Context context) {
          OkHttpClient okHttpClient = new OkHttpClient.Builder()
                  .cache(new Cache(context.getCacheDir(), 5 * 1024 * 1024))
                  .addInterceptor(new Interceptor() {
                      @Override
                      public okhttp3.Response intercept(Chain chain) throws IOException {
                            Response originalResponse = chain.proceed(chain.request());
                            Log.d(TAG,"ORIGINAL RESPPONSE HEADERS "+originalResponse.headers().toMultimap());
  
  
                          Log.d(TAG,"Request is here");
                          if(isNetworkAvailable(context)) {
                              Log.d(TAG,"WE HAVE INTERNET");
                              Log.d(TAG,"If less then 1 minute, should read from cache");
                              int maxAge = 60;
                              return originalResponse.newBuilder()
                                      .header("Cache-Control", "public, max-age=" + maxAge)
                                      .build();
                          }
                          else {
                              Log.d(TAG,"No INTERNET");
                               int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
              return originalResponse.newBuilder()
                      .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                      .build();
                          }
                      }
                  })
                  .build();
          Retrofit retrofit = new Retrofit.Builder()
                  .addConverterFactory(GsonConverterFactory.create())
                  .client(okHttpClient)
                  .baseUrl(BASE_URL)
                  .build();
  
          return retrofit;
      }
  }
  </code></pre>
  
  <p>After that i make a call to the api:</p>
  
  <pre><code>searchMovies("Titanic");
  </code></pre>
  
  <p>And i get the results. After i turn off wifi/3g, when i make the api call again, i get a <code>Unable to resolve host "www.omdbapi.com" No adress assoicatied with hostname.</code></p>
  
  <p>It seems like it does not even check if there is anything in cache, and just tries to do another request, but this time with no internet. I have a log that prints out the reponse headers when im making the call with internet, this is the response : </p>
  
  <pre><code>ORIGINAL RESPPONSE HEADERS {access-control-allow-origin=[*], cache-control=[public, max-age=86400], cf-cache-status=[HIT], cf-ray=[4c6ec52828bb9720-FRA], connection=[keep-alive], content-type=[application/json; charset=utf-8], date=[Sat, 13 Apr 2019 16:20:55 GMT], expires=[Sun, 14 Apr 2019 16:20:55 GMT], last-modified=[Fri, 12 Apr 2019 16:04:42 GMT], server=[cloudflare], set-cookie=[__cfduid=dad69c836be19bd3f9575cc59759f95d21555172455; expires=Sun, 12-Apr-20 16:20:55 GMT; path=/; domain=.omdbapi.com; HttpOnly], transfer-encoding=[chunked], vary=[*, Accept-Encoding], x-aspnet-version=[4.0.30319], x-powered-by=[ASP.NET]}
  </code></pre>
  
  <p>When making the call with no internet, none of the logs in the above code trigger.</p>
  
  <p>As a side note, here are the imports i have in the class:</p>
  
  <pre><code>import android.content.Context;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo;
  import android.util.Log;
  import java.io.IOException;
  import okhttp3.Cache;
  import okhttp3.Interceptor;
  import okhttp3.OkHttpClient;
  import okhttp3.Response;
  import retrofit2.Retrofit;
  import retrofit2.converter.gson.GsonConverterFactory;
  </code></pre>
  
  <p>Any ideas ?</p>