3

I am trying to cache this file: http://media.admob.com/sdk-core-v40.js with HttpResponseCache. Basically i am calling the getFileFromUrl function two times, so that the first time it gets the resource from the internet and the second time it gets it from the cache.

However, for both methods it gives this error: "The resource was not cached!java.io.FileNotFoundException: http://media.admob.com/sdk-core-v40.js". Does this mean that the file was not cached and it can not access it? What is wrong with the code?

enableHttpCaching();

getFileFromURL(url);
getFileFromURL(url);


public static void getFileFromURL(String src) {
     try {
            URL url = new URL(src);
            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
            connection.setUseCaches(true);
            connection.addRequestProperty("Cache-Control", "only-if-cached" );        
            InputStream input = connection.getInputStream();
            System.out.println("The resource was cached!");
        }catch (FileNotFoundException e) {
            System.out.println("The resource was not cached!" + e);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
Jonik
  • 80,077
  • 70
  • 264
  • 372
irena
  • 31
  • 2

1 Answers1

1

From what it looks, this is not caching because the response file has

Cache-Control:max-age=0
Alon Burg
  • 2,500
  • 2
  • 27
  • 32
  • I wonder if `Cache-Control max-stale` in the request would suffice to override that: http://stackoverflow.com/a/11714075/56285 – Jonik Oct 31 '13 at 16:56