3

I want to load a webpage.

private class MyJavaScriptInterface {

    private MyJavaScriptInterface () {
    }

    public void setHtml(String contentHtml) {

        if (contentHtml != null && contentHtml.trim().length() > 0) {
            //Do something
        }
    }
}
private WebViewClient webViewClient = new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {

        view.loadUrl("javascript:window.ResponseChecker.setHtml"
            + "(document.body.innerHTML);");
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    public void onReceivedSslError(WebView view, SslErrorHandler handler,
        SslError error) {
        handler.proceed();
    }

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Log.e("ProcessPayment", "onReceivedError = " + errorCode);
    }

};

I want to handle webpage loading errors. I know that the errors can be obtained in onReceivedError(...) method.

My problem is how can I handle the error without showing Page Not found in webview? (eg: Show a dialog and makes webview blank).

Thanks in Advance.

Michaël
  • 3,679
  • 7
  • 39
  • 64
Devu Soman
  • 2,246
  • 13
  • 36
  • 57

2 Answers2

8

Check as:

 public void onReceivedError(WebView view, int errorCode, 
                           String description, String failingUrl) {
             Log.e("ProcessPayment", "onReceivedError = " + errorCode);

            //404 : error code for Page Not found
             if(errorCode==404){
               // show Alert here for Page Not found
               view.loadUrl("file:///android_asset/Page_Not_found.html");
             }
            else{

              }
           }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • I showed an alert in onReceivedError. But the webview loads the the error page. – Devu Soman Dec 05 '12 at 08:20
  • @DevuSoman : in onReceivedError when error occur set WebView url to your on custom page as `view.loadUrl("file:///android_asset/html_no_copy/Page_Not_found.html");` try this maybe solve your problem – ρяσѕρєя K Dec 05 '12 at 08:44
  • 1
    onReceivedError doesn't get called for a 404. Any error document provided by the web server is loaded. This method is only called for network errors (name not resolved, etc.) See https://code.google.com/p/android/issues/detail?id=32755 – MZB Aug 12 '15 at 02:11
2
public void onReceivedError(WebView view, int errorCode, String description,
        String failingUrl) {

    if (errorCode == ERROR_HOST_LOOKUP || errorCode == ERROR_FILE_NOT_FOUND) {
        // TODO your dialog here
    }
}
Erik Ghonyan
  • 427
  • 4
  • 12