5

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
webView.loadUrl("https://www.easistent.com/urniki/izpis/263/16515/0/0/1");

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.

Community
  • 1
  • 1
Guy
  • 6,414
  • 19
  • 66
  • 136
  • one issue you might face is that this particular page declares 'Cache-Control:max-age=0' which prevents it from being put in cache by the web client (not only that, but also no-cache, expires ...) – njzk2 Sep 06 '13 at 12:18
  • What about this ? http://stackoverflow.com/questions/7598273/how-i-can-save-webview-contents-to-show-even-when-no-network-available – Alexander.Iljushkin Sep 06 '13 at 12:24

2 Answers2

15

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 :

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

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)

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • I already have this code in my app. So you're saying that I might not be able to save cache for this webpage? Are there any other alternatives for saying webview pages to view them without connection? – Guy Sep 06 '13 at 12:54
  • you have to change the page to allow it to be put in cache. – njzk2 Sep 06 '13 at 12:56
  • otherwise you'll have to download the page and display it locally – njzk2 Sep 06 '13 at 12:57
  • I sadly don't have any administration rights, the page isn't mine, so I can't do anything about that. Could you give me any info on how to download it and display it locally? Maybe I could make it download everytime an internet connection is there and that way it would still be updated from time to time – Guy Sep 06 '13 at 12:59
  • your pages is made of 3 items, apparently. a css, an image, and an html file. both css and image should support caching. You need to save the page using any basic http request method, save it locally and point your webview toward it. Apparently the image and css urls are absolute, so you shouldn't need to download them – njzk2 Sep 06 '13 at 13:36
  • webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); – Murali Jul 01 '16 at 15:37
1

@yeradis(How I can Save WebView Contents to show even when no network available?):

Maybe using a cache is the best way... for that you should check http://developer.android.com/reference/android/webkit/WebSettings.html

"Manages settings state for a WebView. When a WebView is first created, it obtains a set of default settings. These default settings will be returned from any getter call. A WebSettings object obtained from WebView.getSettings() is tied to the life of the WebView. If a WebView has been destroyed, any method call on WebSettings will throw an IllegalStateException."

Especifically:

public static final int **LOAD_CACHE_ONLY**

Since: API Level 1 Don't use the network, load from cache only. Use with setCacheMode(int). Constant Value: 3 (0x00000003)

Or

public static final int **LOAD_CACHE_ELSE_NETWORK**

Since: API Level 1 Use cache if content is there, even if expired (eg, history nav) If it is not in the cache, load from network. Use with setCacheMode(int). Constant Value: 1 (0x00000001)

Update 1:

hmm "crappy code of all life" for example:

public static InputStream fetch(String url) throws MalformedURLException,
        IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    HttpResponse response = httpClient.execute(request);
    return response.getEntity().getContent();
}


private static String convertStreamToString(InputStream is)
        throws IOException {

    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    return writer.toString();
}

So, you can fetch a url with InputStream fetch(String url) an then convert that stream to String with private static String convertStreamToString(InputStream is) and save that stream to a file, later you can load that content to a webview... to read later.

Update2:

Or you can do some Java Serialization stuff... cant remember if this work on android xD

Discover the secrets of the Java Serialization API http://java.sun.com/developer/technicalArticles/Programming/serialization/

Community
  • 1
  • 1
Alexander.Iljushkin
  • 4,519
  • 7
  • 29
  • 46