1

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".

WildChild
  • 93
  • 7
  • And what is the result of the second '.getInputStream()'? – greenapps Oct 21 '14 at 10:15
  • @greenapps its a correct InputStream, handled with a normal download request. Basically the first "cached" input stream is never reached. – WildChild Oct 22 '14 at 13:02
  • `long httpCacheSize = 10 * 1024 * 1024; // 10 MiB` and `it prints correctly a cache size of 2 mb` ??? 2MB you mean? but why not 10MB? – greenapps Oct 22 '14 at 13:12
  • `So, basically, the cache process works great,`. Did you check if the http directory is created? Did you check if it has/had contents? – greenapps Oct 22 '14 at 13:14
  • But i wonder: what is the purpose of such a cache? For what to use it? – greenapps Oct 22 '14 at 13:15
  • @greenapps the 2mb size was referred as the real cached content. The 10mb is the maximum size of cache permitted. The purpose of the cache is to reuse the response even on offline situations or simply to speed up the HTTP calls. I did it in the iOS version. – WildChild Oct 24 '14 at 14:29
  • @greenapps To answer your question about the http directory... As far as I know, there is no methods to check how many and what directories are created in the cache. – WildChild Oct 24 '14 at 14:30

1 Answers1

2

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.

WildChild
  • 93
  • 7