Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, December 18, 2015

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

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


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.

Answer by Kishor Pawar for how to load or retrieve a webpage in both online and offline mode in android application?


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.

Answer by Gregory for how to load or retrieve a webpage in both online and offline mode in android application?


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);  

Answer by Nikhil Borad for how to load or retrieve a webpage in both online and offline mode in android application?


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.

Answer by Osvaldo Bringaz for how to load or retrieve a webpage in both online and offline mode in android application?


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

Answer by Akhil Jayakumar for how to load or retrieve a webpage in both online and offline mode in android application?


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.

Answer by youngdero for how to load or retrieve a webpage in both online and offline mode in android application?


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:

      


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.