HttpResponseCache does not cache files

841 Views Asked by At

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();
        }
    }
1

There are 1 best solutions below

1
On

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

Cache-Control:max-age=0