how to load or retrieve a webpage in both online and offline mode in android application?
Asked Answered
L

6

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.

Legalism answered 25/11, 2015 at 11:42 Comment(1)
Have you tried this setting viewer.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT) to webview?Cuticle
S
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.

Scrope answered 16/12, 2015 at 7:1 Comment(0)
P
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.

Publicity answered 16/12, 2015 at 6:6 Comment(0)
S
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.

Scrope answered 16/12, 2015 at 7:1 Comment(0)
W
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.

Winonawinonah answered 12/12, 2015 at 6:56 Comment(0)
E
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);
Ernie answered 12/12, 2015 at 17:44 Comment(0)
E
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

Ebonieebonite answered 16/12, 2015 at 6:50 Comment(0)
H
-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"/>
Heterosis answered 19/12, 2015 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.