title: How to show cache only if the device is offline in Volley
body: <p>I am using Volley and I want to cache response(no server control-cache headers).</p>
  
  <p>I want to show the cached value only if there is no internet.</p>
  
  <p>What I tried:</p>
  
  <pre><code>private Entry getCachedEntry(NetworkResponse response) {
      // entry that will be passed to the response instance
      Entry cachedEntryForRequest = HttpHeaderParser
              .parseIgnoreCacheHeaders(response);
  
      // The request queue cache
      Cache cache = BaseApplication.getInstance().getRequestQueue()
              .getCache();
  
      // gets the cached entry for the request
      Entry cachedItems = cache.get(GetEvents.KEY_CACHE);
  
      if (NetworkChecker.isOnline(mContext)) {
  
          // puts the entry with the new data to the cache
          cache.put(GetEvents.KEY_CACHE, cachedEntryForRequest);
  
          // return null so the new data to be shown
          cachedEntryForRequest = null;
      } else {
          // show the cached data if the device is offline
          if (cachedItems != null) {
              cachedEntryForRequest.data = cachedItems.data;
          } else {
              cachedEntryForRequest = null;
          }
      }
  
      return cachedEntryForRequest;
  }
  
      protected Response&lt;T&gt; parseNetworkResponse(NetworkResponse response) {
      try {
          String json = new String(response.data,
                  HttpHeaderParser.parseCharset(response.headers));
  
          Log.v(TAG, "response: " + json);
  
          Object fromJson = mGson.fromJson(json, mType);
  
          return Response.success((T) fromJson, getCacheEntry(response));
      } catch (UnsupportedEncodingException e) {
          return Response.error(new ParseError(e));
      } catch (JsonSyntaxException e) {
          return Response.error(new ParseError(e));
      }
  }
  </code></pre>
  
  <p>This is my own solution but I'm doing something wrong because it's showing the cache result even if the device is online?!?!?</p>