Home » Android » caching – Why doesn't android Webview check for cache by default?

caching – Why doesn't android Webview check for cache by default?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a webview using application caching. I launched the app with a connection to the network in order to load/cache the page. Now, after turning the wifi (and 3g – no network connection) off, and launch the app, I get shown the “android could not load the page” page. Oddly, if I then reload the webview, it loads up the page from cache. Why doesn’t it load this page from the cache if it can’t connect to the network?

I’m using HTML5 Application Cache with cache.manifest. Here’s my webview code to enable this:

webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setAppCacheMaxSize(1024*1024*8);
webview.getSettings().setAppCachePath("/data/data/com.my.package/cache");
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAppCacheEnabled(true);

I tried adding in:

weview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 

but it didn’t help.

How to&Answers:

Check to see if the URL you’re requesting isn’t a redirect. If it is, the cache will be pointing to that redirect as well, and the WebView won’t read the resulting redirect from the cache.

It took me some time to figure out that this was what was happening in my case.