displaying cached version of webview in android
Asked Answered
S

3

11

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);
Shiest answered 11/12, 2011 at 22:21 Comment(0)
R
7

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(...)
Risk answered 20/12, 2011 at 10:1 Comment(1)
this is probably the "cleanest" solution, so ill give you the bounty points :)Shiest
D
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.


Drawbar answered 14/12, 2011 at 8:48 Comment(4)
@Shiest did you try to change LOAD_DEFAULT to LOAD_NORMALDrawbar
still just has generic "Web Page not available" message when the net connection is down when i use LOAD_NORMALShiest
I created a "workaround" where i load up the LOAD_NO_CACHE version, if that fails then in onReceivedError i load the LOAD_CACHE_ONLY version .. i have this working, but its quite a dodgy work around, i was hoping for a clean solution. its a bit silly it doesnt have native support for this ..Shiest
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 loopDrawbar
S
1

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.

Scandalize answered 20/12, 2011 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.