How to handle errors inside webview?

12,888

Solution 1

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{

              }
           }

Solution 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
    }
}
Share:
12,888
Devu Soman
Author by

Devu Soman

Updated on July 01, 2022

Comments

  • Devu Soman
    Devu Soman almost 2 years

    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.