When WebView is loading url, user can lose access to internet (for example, he work with router, but money in account is ended).
In this case WebView show own html "Webpage not available".
I try to use custom WebViewClient and callback:
#Override
public void onReceivedError(android.webkit.WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
but it`s not called.
How to handle offline when loading url in WebView?
P.S. Do not offer solutions to check the network status, please. I am interested only WebView.
Try this :
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(String.valueOf(errorCode), description);
// API level 5: WebViewClient.ERROR_HOST_LOOKUP
if (errorCode == -2) {
String summary = "<html><body style='background: black;'><p style='color: red;'>Unable to load information. Please check if your network connection is working properly or try again later.</p></body></html>"; view.loadData(summary, "text/html", null);
return;
}
// Default behaviour
super.onReceivedError(view, errorCode, description, failingUrl);
}
Hey do this for give popup to user that check your data..
StackOverflow link
AndroidHive link
I found the reason why not work error callbacks of my WebView:
getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
After removing, this method:
#Override
public void onReceivedError(android.webkit.WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
started working again
1.Save yout error template as error.html in asset
2.when error occurs load template from asset
webview.loadUrl("file:///android_asset/error.html");
Related
I have a webview that is loading data and caching it , in the code below this cached data is being loaded if it is found
However I want the webview to reload from the network if it finds a change in the url rather than just loading the recent cached item
Is there a way for me to compare the current url with the cached one , or someway to have the webview load the cache if the same or the new one when different
wvStructue.settings.javaScriptEnabled=true
wvStructue.settings.loadWithOverviewMode = true
wvStructue.settings.useWideViewPort = true
wvStructue.settings.databaseEnabled = true
wvStructue.settings.domStorageEnabled = true
wvStructue.settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
wvStructue.settings.builtInZoomControls = true
wvStructue.settings.displayZoomControls = false
Refer this link
Set webview cache mode LOAD_CACHE_ELSE_NETWORK.
Override WebViewClient methods :
#SuppressWarnings("deprecation")
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (view.getUrl().equals(failingUrl)){
showInternetConnectionError();
}
super.onReceivedError(view, errorCode, description, failingUrl);
}
#TargetApi(android.os.Build.VERSION_CODES.M)
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
onReceivedError(view, error.getErrorCode(), error.getDescription().toString(), request.getUrl().toString());
}
I was making a webview application for my local network. I have two static IPs. All I want to do is to load second if 1st one is down. (Because one of them is always running).
What I'm doing is to check it in onRecievedError() function and change the URL value, then call the onCreate() again like this.
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
url1="http://192.168.43.XXX"; //here XXX is used just to hide my IP from public
onCreate(new Bundle());
}
the onCreate() method has to render the new URL but my application closes itself upon this call. Why is this so?
Please help because I'm a beginner.
use this code
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
if(failingUrl.equals(url1))
{
//you can use load url if fail first url
// dont't use onCreate again
webview.loadUrl(url2);
}
}
Context
In the Android SDK 23 onReceivedError(WebView view, int errorCode, String description, String failingUrl) has been deprecated and replaced with onReceivedError(WebView view, WebResourceRequest request, WebResourceError error). However as per documentation:
Note that unlike the deprecated version of the callback, the new
version will be called for any resource (iframe, image, etc), not just
for the main page
Problem
We have an app where in the deprecated onReceivedError method there is a code to display a native view instead of letting the user see the error in the WebView.
We would like to replace the deprecated onReceivedError method by the new method. But we don't want to display the native view for errors for any resource, just for the main page.
Question
How can we identify in the new onReceivedError that the error is not from the main page?
PS 1: We would prefer not having a solution like this to store the main url and check it against the failing url.
PS 2: If the solution is to just use the deprecated method, what's the guarantee that it will still be called for new Android versions?
WebResourceRequest has isForMainFrame() method for your scenario which is available starting from API version 21:
Source: https://developer.android.com/reference/android/webkit/WebResourceRequest.html
You don't have to store the original URL. You can get it from the WebView passed to the onReceivedError method. It's always the full URL of the current page that the user sees. So, you don't have to worry about them navigating to different pages.
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
if (request.getUrl().toString().equals(view.getUrl())) {
notifyError();
}
}
you can use like as codes:
WebView wv = (WebView) findViewById(R.id.webView);
wv.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
// here your custom logcat like as share preference or database or static varible.
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
Best of luck!
I am using an authenticated webview in my app. Currently when we enter the search term in the search box,it is added to the url (say http.cookies.com/something/SEARCHTERM/something) and the url is called in the webview which loads the url as follows:
if(!StringUtils.isEmpty(URL)){
if(URLUtil.isValidUrl(URL)){
mWebview.loadUrl(URL);
}else{
mWebview.loadUrl("http://www.google.com");
}
However, even if the url is invalid, it still loads the invalid url which shows up as "412- precondition set and hence page cannot be displayed".Is there a way I can detect if this search term or url is valid and display it in the webview , if valid) as the requested url , if not valid, i display some other webview like say google.com?
Thanks!
justin
You can use the Webview's WebViewClient to catch the error and do whatever you see convenient. Here is a sample:
yourWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
I'm trying to display web page using WebView in android, when a particular page doesn't load, it displays error message with URL, saying that abcd.com is not available, how do I replace the error message with my custom message instead of displaying the URL?
storeLocator.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
storeLocator.loadUrl("file:///assets/error.html");
}
});
storeLocator.loadUrl("http://goog.c");
You can create the html page whatever you want to show whenever the error comes or your page is not available ,the code is given here-
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webview.loadUrl("file:///android_asset/myerrorpage.html");
}
});