Android Webview Onreceivederror()

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:

Sample Image

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



Leave a reply



Submit