18

I need to load and retrieve a HTML webpage in internal or external memory of android device. What i need is to download and retrieve a webpage in android using web-view.

There is lot of repeated questions similar to downloading or saving the webpage. But none of the answers helped me. Guide me!

Thanks in advance.

  • Have you tried this setting viewer.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT) to webview? – sha Dec 15 '15 at 01:38

6 Answers6

6

Get the HTML code from loaded WebView

Save String as html file android

With above two link you can achieve your requirement, you can store html content into file and then store into internal storage then you can load in offline mode.

Community
  • 1
  • 1
Nikhil Borad
  • 2,065
  • 16
  • 20
6

I don't know is this solution suitable for your.if you want to view a website in offline mod and online mod you can use a web crawler to fetch all data from website.Here is an example project for android web crawler.after that you can load a website from url or local memory based on your internet Availability.

Akhil Jayakumar
  • 2,262
  • 14
  • 25
0

A WebView can load and render remote and local html pages plus it also support 'data' scheme URL.

Storage Options

Shared Preferences (not useful in your case)

Store private primitive data in key-value pairs.

Internal Storage ( Recommended for your case )

Store private data on the device memory.

External Storage ( You can use in you case but not recommended )

Store public data on the shared external storage.

SQLite Databases ( You can use in you case but not recommended )

Store structured data in a private database.

Network Connection ( You can use in you case but will not work in offline mode )

Store data on the web with your own network server.

Kishor Pawar
  • 3,386
  • 3
  • 28
  • 61
0

You might want to try to use shouldInterceptRequestmethod of WebViewClient which use set to your WebView. It allows you to know all resources which are loaded during html loading procedure. So you can do something like this:

WebViewClient webViewClient = new WebViewClient() {
    @TargetApi(VERSION_CODES.LOLLIPOP)
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        WebResourceResponse response = null;
        if ("GET".equals(request.getMethod())) {
            try {
                response = getWebResponse(view.getContext().getApplicationContext(), request.getUrl().toString());
            } catch (Exception e) {
                Log.e(TAG, "Error while overriding getting web response", e);
            }
        }
        return response;
    }

    @SuppressWarnings("deprecation")
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        WebResourceResponse response = null;
        try {
            response = getWebResponse(view.getContext().getApplicationContext(), url);
        } catch (Exception e) {
            Log.e(TAG, "Error while overriding getting web response", e);
        }
        return response;
    }

    WebResourceResponse getWebResponse(Context context, String url) {
        // YOUR IMPLEMENTATION that will save resource located at passed url
    }
}
webView.setWebViewClient(webViewClient);
GregoryK
  • 3,011
  • 1
  • 27
  • 26
0

save the webpage into cache memory;

WebView webView = new WebView( context );
webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
webView.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default

Check this link WebView load website when online, load local file when offline

Community
  • 1
  • 1
Osvaldo Bringaz
  • 127
  • 1
  • 7
-1

That sounds like a simple webview caching mechanism to me.

try this

  WebView webView = new WebView( context );
webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
webView.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default

if ( !isNetworkAvailable() ) { // loading offline
    webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}

The method isNetworkAvailable() checks for an active network connection:

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

webView.loadUrl( "http://www.google.com" );

Finally, don't forget to add the following three permissions to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
youngdero
  • 382
  • 2
  • 16