I'm trying to employ HttpResponseCache to cache each HTTP-request sent with HttpURLConnection.
But even if request goes to a resource 2'nd time the hit count always equals zero. Why it doesn't hit the cache?
Hit count:
import android.net.http.HttpResponseCache;
...
HttpResponseCache.getInstalled().getHitCount()
Usage:
for(byte i = 0; i < 2; i++){
// Cache-Control:max-age=300
HttpURLConnection conn = (HttpURLConnection) new URL("http://redbot.org/").openConnection();
conn.setUseCaches(true);
if(i == 1){
try {
conn.addRequestProperty("Cache-Control", "only-if-cached");
InputStream cached = conn.getInputStream();
// the resource was cached! show it
Log.i(TAG, "cached");
} catch (FileNotFoundException e) {
// the resource was not cached
Log.i(TAG, "NOT cached");
}
}
}
This is how I enable the cache:
File httpCacheDir = new File(this.getCacheDir(), "http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);