androidhttpcachingresponsehttpresponsecache

Android HttpResponseCache not working - FileNotFoundException


I have a problem caching http(s) responses from web server in Android, documentation is poor, so I'm asking for help here. Here's my code:

    String encodedString = String.format("jsonData=%s", URLEncoder.encode(json, "UTF-8"));

    URL urlConn = new URL(url+"?"+encodedString);

    HttpURLConnection cachedUrlConnection = (HttpURLConnection) urlConn.openConnection();
    cachedUrlConnection.setUseCaches(true);
    cachedUrlConnection.setDoInput(true);
    cachedUrlConnection.setDoOutput(true);
    cachedUrlConnection.setRequestProperty("charset", "utf-8");
    cachedUrlConnection.setRequestMethod("GET");

    cachedUrlConnection.addRequestProperty("Cache-Control", "only-if-cached");


    InputStream is = null;
    try {
        is = cachedUrlConnection.getInputStream();

        Log.i("_INFO","------CACHE FOUNDED");

    } catch (FileNotFoundException e){
        e.printStackTrace();

        HttpURLConnection nonCachedUrlConnection = (HttpURLConnection) urlConn.openConnection();
        nonCachedUrlConnection.setUseCaches(true);
        nonCachedUrlConnection.setDoInput(true);
        nonCachedUrlConnection.setDoOutput(true);
        nonCachedUrlConnection.setRequestProperty("charset", "utf-8");
        nonCachedUrlConnection.setRequestMethod("GET");
        nonCachedUrlConnection.addRequestProperty("Cache-Control", "max-stale=" + (60 * 60 * 24));

        Log.i("_INFO","-------CACHE NOT FOUNDED");

        is = nonCachedUrlConnection.getInputStream();
    }

Also, I've already installed the cache on Application onCreate method as follows:

try {
        long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
        File httpCacheDir = new File(getExternalCacheDir(), "http");
        Class.forName("android.net.http.HttpResponseCache")
                .getMethod("install", File.class, long.class)
                .invoke(null, httpCacheDir, httpCacheSize);

        Log.i("_INFO", "HttpResponseCache enabled");

    } catch (Exception httpResponseCacheNotAvailable) {
        Log.d("_INFO", "HTTP response cache is unavailable.");
    }

And if I print the cache size, after the application re-start, it prints correctly a cache size of 2 mb (so it caches correctly). I flush the cache after all the HTTP calls, as follows:

HttpResponseCache cache = HttpResponseCache.getInstalled();
        if(cache != null) {
            Log.i("_INFO", "CACHED FLUSHED WITH " + cache.size());
            cache.flush();
        }

So, basically, the cache process works great, BUT I'm not able to GET the cached response when I getInputStream. My logcat always print "CACHE NOT FOUNDED".


Solution

  • After a while I end up with the solutions, seems like there was too much parameters on my HTTP call, or maybe a wrong one.

    With only:

    cachedUrlConnection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);
    

    this one, the cache works charming.