我正在尝试使用 HttpResponseCache 缓存此文件:http : //media.admob.com/sdk-core-v40.js。基本上我调用 getFileFromUrl 函数两次,以便第一次从 Internet 获取资源,第二次从缓存中获取资源。
但是,对于这两种方法,它都会出现此错误:“资源未缓存!java.io.FileNotFoundException: http: //media.admob.com/sdk-core-v40.js ”。这是否意味着该文件未缓存并且无法访问?代码有什么问题?
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();
}
}