0

I have a Android app that uses the webview. There is a java backend server that serves up all the front end files and builds the cache manifest over apache tomcat.

My problem is that I'm unsure how to get the webview to load from the app cache when the server is not connected, but the device is otherwise connected to the internet.

If I go into the app code, I can force it to the use the cache with

webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);

So I know my files are appropriately being cached. I also tried solutions from other stack overflow questions which had me check the status of the network and setting the cache mode based on the network status like this:

     cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
     if(cm.getActiveNetworkInfo().isConnected()) ....

However, this doesn't work in the case where the device is online, but my server is down. It still says the network is fine in that case and doesn't load from the cache.

The ideal solution would be that when the front end webview tries to access a resource over the network and it's unavailable, it just loads from the cache, which is exactly how it works in a normal browser. But I can accept workarounds, like have the app detect the network connection to my specific server and changing the cache mode accordindly.

Anyone know the best approach for this problem? Thanks.

1 Answer 1

2

So the problem seemed to be that android just doesn't use the HTML5 app cache at all unless you tell it to, which was just really throwing my thinking off. I solved it by using the following code:

final WebSettings webviewSettings = webView.getSettings();
final String appCachePath = this.getCacheDir().getAbsolutePath();
webviewSettings.setDomStorageEnabled(true);
webviewSettings.setAppCachePath(appCachePath);
webviewSettings.setAllowFileAccess(true);
webviewSettings.setAppCacheEnabled(true);
webviewSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

Then it behaved how I expected the app cache to behave. Ie, it loads resources from the cache if they can't be loaded over the network, as specified by the CACHE section in the cache manifest.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.