Webview Load Website When Online, Load Local File When Offline

WebView load website when online, load local file when offline

That sounds like a simple webview caching mechanism to me.

The following should do what you are looking for:

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

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

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

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"/>

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.

Implement an Offline-mode for the Windows UWP WebView

You can save your web page in the local file for example as a string, to return the response body as a string, you can create a new instance of HttpClient.

Here is a method, in the NavigationCompleted event of your WebView, code like this:

private async void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
if (args.IsSuccess == true)
{
try
{
using (HttpClient client = new HttpClient())
{
if (args.Uri != null && args.Uri.ToString() == "https://developer.microsoft.com/en-us/windows/develop")
{
var htmlstring = await client.GetStringAsync(args.Uri);
StorageFolder local = ApplicationData.Current.LocalFolder;
StorageFile file = await local.CreateFileAsync("offline-web.html", CreationCollisionOption.ReplaceExisting);
using (StreamWriter writer = new StreamWriter(await file.OpenStreamForWriteAsync()))
{
writer.Write(htmlstring);
}
}
}
}
catch (Exception ex)
{
}
}
else
{
StorageFolder local = ApplicationData.Current.LocalFolder;
StorageFile file = await local.GetFileAsync("offline-web.html");
if (file != null)
{
using (StreamReader reader = new StreamReader(await file.OpenStreamForReadAsync()))
{
var htmlstring = await reader.ReadToEndAsync();
sender.NavigateToString(htmlstring);
}
}
else
{
MessageDialog dlg = new MessageDialog("Please check your internet", "Warning");
await dlg.ShowAsync();
}
}
}

You can also try other methods, for example save the html file, and navigate to the local file. And as @Amod Gokhale said, if there is some dynamic objects need to be loaded with Internet, then in the offline-mode, these objects will not be loaded even you saved the html page unless you also saved all the resources of these dynamic objects.



Related Topics



Leave a reply



Submit