displaying cached version of webview in android

半世苍凉 提交于 2020-01-02 01:22:29

问题


I am trying to get the HTML5 offline cached version of a website to display when the network is down inside of a webview.

I have overridden onReceivedError ok, and when the network is down this method is called. Problem is that it displays the generic "Web Page is not available" message.

How can I get it to display the HTML5 cached version of the page? The offline storage of the webapp is definately working, as it works fine in the desktop version of Firefox and Chrome.

I know I can call loadData into the view manually in onReceivedError, but im not sure where I can get the HTML5 cached value from.

Note: If I set some dummy data in loadData such as view.loadData(Uri.encode("<html><div>Page load failed</div></html>"), "text/html", "UTF-8"); and then click back (by detecting back event and calling webview.goBack(); then the cached version of the page is displayed ok.

Here are some lines of code I added to setup the webview:

webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webview.getSettings().setAppCacheMaxSize(1024*1024*8);                         
webview.getSettings().setAppCachePath("/data/data/com.stuff.android/cache");
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAppCacheEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setJavaScriptEnabled(true);

回答1:


Try to find out the network status using

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

(Detect whether there is an Internet connection available on Android)

This also needs

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in your AndroidManifest.xml

Now you can set the cache behaviour either to LOAD_CACHE_ONLY or to LOAD_NO_CACHE, depending on whether a network connection is available, with

webview.getSettings().setCacheMode(...)



回答2:


I think a good solution would be to use LOAD_NORMAL and onReceivedError Navigate BACK. I think this will load the cache according to the documentation (not sure if I remember correctly) but be carefull no to get stuck in an infinite loop


It is weird.. According to the documentation:

Override the way the cache is used. The way the cache is used is based on the navigation option. For a normal page load, the cache is checked and content is re-validated as needed. When navigating back, content is not revalidated, instead the content is just pulled from the cache. This function allows the client to override this behavior.

However the behavior you want does not seem to be one of these:

  1. LOAD_CACHE_ELSE_NETWORK

    Use cache if content is there, even if expired (eg, history nav) If it is not in the cache, load from network.

  2. LOAD_CACHE_ONLY

    Don't use the network, load from cache only.

  3. LOAD_DEFAULT

    Default cache usage pattern

  4. LOAD_NORMAL

    Normal cache usage pattern

  5. LOAD_NO_CACHE

    Don't use the cache, load from network


I do not know if you can subclass the WebView to get the desired flow.





回答3:


Does it not work if you simply let the browser handle this? Specify the manifest in the HTML tag like this:

<html manifest="mycache.appcache">

...and the browser should automatically use it when there's no connection available, you shouldn't need to change any settings at all.



来源:https://stackoverflow.com/questions/8467998/displaying-cached-version-of-webview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!