title: Android set up Volley to use from cache
body: <p>I'm trying to create and use a cache for a server JSON response. </p>
  
  <p>For example:</p>
  
  <p>cache JSON objects to internal memory and use that when we don't have an internet connection. </p>
  
  <p>In the following sample code, <strong>I can not find any document about how to cache it with <code>Volley</code> and reuse that when server header for update again don't expire.</strong> </p>
  
  <p>Like this: set expiration to header and use cache and try to load again after expiration.</p>
  
  <p>I'm trying to set cache mechanism for this method:</p>
  
  <pre><code>private void makeJsonArryReq() {
      JsonArrayRequest req = new JsonArrayRequest(Const.URL_JSON_ARRAY,
              new Response.Listener&lt;JSONArray&gt;() {
                  @Override
                  public void onResponse(JSONArray response) {
                      msgResponse.setText(response.toString());
                  }
              }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
          }
      });
      AppController.getInstance().addToRequestQueue(req,tag_json_arry);
  }
  </code></pre>
  
  <p><strong>Cache method:</strong></p>
  
  <pre><code>public static Cache.Entry parseIgnoreCacheHeaders(NetworkResponse response) {
      long now = System.currentTimeMillis();
  
      Map&lt;String, String&gt; headers = response.headers;
      long serverDate = 0;
      String serverEtag = null;
      String headerValue;
  
      headerValue = headers.get("Date");
      if (headerValue != null) {
          serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
      }
  
      serverEtag = headers.get("ETag");
  
      final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
      final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
      final long softExpire = now + cacheHitButRefreshed;
      final long ttl = now + cacheExpired;
  
      Cache.Entry entry = new Cache.Entry();
      entry.data = response.data;
      entry.etag = serverEtag;
      entry.softTtl = softExpire;
      entry.ttl = ttl;
      entry.serverDate = serverDate;
      entry.responseHeaders = headers;
  
      return entry;
  }
  </code></pre>