Whiz Whiz - 1 year ago 478
Android Question

Saving WebView page to cache

I have an app with WebView and I want the app to save the website the first time it is connected to the internet so that a further connection is no longer needed. I know some people are saving WebView pages to cache. I've done some research and I found some answers like this one.

But the problem is that I would need some example code on how to do this. Could someone give me an example on how to save a webpage .html file to external storage on Android?

This is the only code i've got at the moment to load a webpage.

//Connecting to UI elements
webView = (WebView) findViewById(R.id.webView1);

//Loading WebView URL

I need some example code. I've seen a lot of documentation and guides, examples on this online but nothing I do works. I'd really appreciate a lot if someone gave me an example with comments.

Answer Source

You can directly use the WebView cache.

Typically, this is activated with the WebSettings.setCacheMode, using the mode LOAD_CACHE_ELSE_NETWORK

Use cached resources when they are available, even if they have expired.

Use like so :


However, in your case, the page declares a 'no-cache' option, which may prevent the WebView to store the page in cache altogether.

If you have the hand on the server side, you can also use the Application Caches API (see http://www.html5rocks.com/en/tutorials/appcache/beginner/ for more details on how that works)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download