1. Download our Official Android App: Forums for Android!

Apps Caching of HTTP requests (HttpResponseCache)

Discussion in 'Android Development' started by surlac, Jan 14, 2014.

  1. surlac

    surlac Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    25
    Joined:
    Jun 17, 2012

    Jun 17, 2012
    25
    4
    16
    Male
    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 (Text):
    1. HttpResponseCache.getInstalled().getHitCount()
    Usage:
    Code (Text):
    1. for(int i = 0; i < 2; i++){
    2. try {
    3.     HttpURLConnection connection = (HttpURLConnection) new URL("http://www.google.com").openConnection();
    4.         connection.setUseCaches(true);
    5.         if(i == 1){
    6.             try {
    7.                 connection.addRequestProperty("Cache-Control", "only-if-cached");
    8.                 InputStream cached = connection.getInputStream();
    9.                 // the resource was cached! show it
    10.                 Log.i(TAG, "cached");
    11.             } catch (FileNotFoundException e) {
    12.                 // the resource was not cached
    13.                 Log.i(TAG, "NOT cached");
    14.             }
    15.         }
    16.     } catch (IOException e) {
    17.         e.printStackTrace();
    18.     }
    19. }
    20.  
    On 2'nd run I have "NOT cached" in console and hit count == 0.
     

    Advertisement

  2. Rukbat

    Rukbat Extreme Android User
    Rank:
    None
    Points:
    943
    Posts:
    10,391
    Joined:
    Jan 16, 2012

    Jan 16, 2012
    10,391
    2,897
    943
    Male
    Being retired and writing Tasker tasks.
    Fayetteville, NC, USA
    If the page has a no-cache header, you won't cache it. That's one possibility.
     
  3. surlac

    surlac Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    25
    Joined:
    Jun 17, 2012

    Jun 17, 2012
    25
    4
    16
    Male
    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?
     

Share This Page

Loading...