• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Caching of HTTP requests (HttpResponseCache)

surlac

Newbie
Hi!
I'm trying to employ HttpResponseCache to cache each HTTP-request sent with HttpURLConnection.

Could somebody tell me why I get hit count always zero?
Code:
HttpResponseCache.getInstalled().getHitCount()

Usage:
Code:
for(int i = 0; i < 2; i++){
try {
    HttpURLConnection connection = (HttpURLConnection) new URL("http://www.google.com").openConnection();
        connection.setUseCaches(true);
        if(i == 1){
            try {
                connection.addRequestProperty("Cache-Control", "only-if-cached");
                InputStream cached = connection.getInputStream();
                // the resource was cached! show it
                Log.i(TAG, "cached");
            } catch (FileNotFoundException e) {
                // the resource was not cached
                Log.i(TAG, "NOT cached");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
On 2'nd run I have "NOT cached" in console and hit count == 0.
 
Thanks Rukbat. I've tried "http://redbot.org/" which has "Cache-Control:max-age=300", cache should be valid for 300 seconds, but the result is same "NOT cached".
Can you put me right?
 
Back
Top Bottom