Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpResponseCache does not cache files

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();
        }
    }
like image 527
irena Avatar asked May 12 '26 14:05

irena


1 Answers

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

Cache-Control:max-age=0
like image 97
Alon Burg Avatar answered May 14 '26 04:05

Alon Burg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!