title: How to use setCache for online data OkHttpClient in Android
body: <p>I want to show 3 <code>fragments</code> in my <code>Activity</code> and load data from <strong><code>json</code></strong> in any <code>fragments</code>! I need to show website data into <code>Recyclerview</code> with <code>OkHTTP v3</code> library. <br>
  I want to show this data for offline, I mean, if user turn off data/wifi show this datas for <strong>offline</strong>. but i do not want use <strong>SQLite Database</strong>!<br>
  For this idea i use <strong>okHttpClient</strong> <code>cache</code> , but not found <code>setCache</code> for client and show me this Error : <a href="https://s32.postimg.org/78got1azp/image.jpg" rel="nofollow">Image link</a></p>
  
  <p><strong>MainActivity codes:</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      private RecyclerView recyclerView;
      private ArrayList&lt;AndroidVersion&gt; data;
      private DataAdapter adapter;
      private static Context context;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          context = this;
  
          initViews();
      }
  
      private void initViews() {
          recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
          recyclerView.setHasFixedSize(true);
          RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
          recyclerView.setLayoutManager(layoutManager);
          loadJSON();
      }
  
      private void loadJSON() {
  
          OkHttpClient client = new OkHttpClient();
          client.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);
  
          //setup cache
          File httpCacheDirectory = new File(context.getCacheDir(), "responses");
          int cacheSize = 10 * 1024 * 1024; // 10 MiB
          Cache cache = new Cache(httpCacheDirectory, cacheSize);
  
          //add cache to the client
          client.setCache(cache);
  
          Retrofit retrofit = new Retrofit.Builder()
                  .baseUrl("http://api.learn2crack.com")
                  .client(client)
                  .addConverterFactory(GsonConverterFactory.create())
                  .build();
          RequestInterface request = retrofit.create(RequestInterface.class);
          Call&lt;JSONResponse&gt; call = request.getJSON();
          call.enqueue(new Callback&lt;JSONResponse&gt;() {
              @Override
              public void onResponse(Call&lt;JSONResponse&gt; call, Response&lt;JSONResponse&gt; response) {
  
                  JSONResponse jsonResponse = response.body();
                  data = new ArrayList&lt;&gt;(Arrays.asList(jsonResponse.getAndroid()));
                  adapter = new DataAdapter(data);
                  recyclerView.setAdapter(adapter);
              }
  
              @Override
              public void onFailure(Call&lt;JSONResponse&gt; call, Throwable t) {
                  Log.d("Error", t.getMessage());
              }
          });
      }
  
      private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
          @Override
          public okhttp3.Response intercept(Chain chain) throws IOException {
              okhttp3.Response originalResponse = chain.proceed(chain.request());
              if (isNetworkAvailable(context)) {
                  int maxAge = 60; // read from cache for 1 minute
                  return originalResponse.newBuilder()
                          .header("Cache-Control", "public, max-age=" + maxAge)
                          .build();
              } else {
                  int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
                  return originalResponse.newBuilder()
                          .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                          .build();
              }
          }
      };
  
      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();
      }
  
  }
  </code></pre>
  
  <p>How can i fix this and use okHttpClient cache?</p>