Web view onReceivedError is handled but still showing web page not available
Because onReceivedError
called before onPageFinished
, so your sadSmiley
and errorText
gone.
Try below code,
boolean errorOccurred = false; // Global variable
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
hideError();
showProgress();
Toast.makeText(Test.this, "start loading", Toast.LENGTH_SHORT).show();
errorOccurred=false;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!errorOccurred) {
hideError();
}
hideProgress();
Toast.makeText(Test.this, "Web view was loaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
errorOccurred = true;
hideProgress();
showError();
Toast.makeText(Test.this, "Could not load your page", Toast.LENGTH_SHORT).show();
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(Test.this, "error", Toast.LENGTH_SHORT).show();
}
});
How to detect errors only from the main page in new onReceivedError from WebViewClient
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
can't see onReceivedError in webview
Add the method:
onReceivedError(WebView view, int errorCode, String description,
String failingUrl)
This method was used in API level 22-.
mywebsite.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view ,String url)
{
pd_loading.setVisibility(View.GONE);
mywebsite.setVisibility(View.VISIBLE);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
mywebsite.loadUrl("about:blank");
pd_loading.setVisibility(View.GONE);
dialog.show();
Toasty.error(Home.this,"Failed",Toast.LENGTH_SHORT,true).show();
}
//For versions < Android M (API 23).
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mywebsite.loadUrl("about:blank");
pd_loading.setVisibility(View.GONE);
dialog.show();
Toasty.error(Home.this,"Failed",Toast.LENGTH_SHORT,true).show();
}
});
Related Topics
Calling Android Dialog Without It Fading the Background
Adding Watermark Bitmap Over Video in Android: 4.3's Mediamuxer or Ffmpeg
Create/Copy File in Android Q Using Mediastore
Use Roboto Font in App with Minimum API Level 14
Using Android to Submit to a Google Spreadsheet Form
Getting Started with Open Nfc Emulator
Enabling General JavaScript in Webviewclient
Cannot Resolve Method 'Getsupportfragmentmanager ( )' Inside Fragment
How to Extract the Text from the Selected Item on the Listview
Bitmap Is Returning Null from Bitmapfactory.Decodefile(Filename)
Start a Service in a Separate Process Android
Google Sceneform - Is It Deprecated? Any Replacement
Scrolling with Multiple Listviews for Android
Viewpager Intercepts All X-Axis Ontouch Events. How to Disable
Illegalstateexception: Can't Change Container Id of Fragment
Mkdir() Works While Inside Internal Flash Storage, But Not Sd Card