0

I have this code:

    WebChromeClient webViewChromeClient = new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress){
        }
    };

    webView.setWebChromeClient(webViewChromeClient);

    WebViewClient webViewClient = new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        } 
    };

And I want to handle errors (404 or error if connection broken) in such a way that in the view showed nothing.

How to do it?

Nips
  • 13,162
  • 23
  • 65
  • 103

1 Answers1

-1

WebView frequently calls application callbacks (via WebViewClient and WebChromeClient), From onReceivedError implement the function to catch and define the Thrown Exeption

  @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
             
            Log.i(TAG, "GOT Page error : code : " + errorCode + " Desc : " + description);
            showError(WebviewActivity.this, errorCode);
            //TODO We can show customized HTML page when page not found/ or server not found error. 
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

Then using the following showError method you can understand what kind of exception has occured.

    private void showError(Context mContext, int errorCode) {
    //Prepare message
    String message = null; 
    String title = null;
    if (errorCode == WebViewClient.ERROR_AUTHENTICATION) {
        message = "User authentication failed on server";
        title = "Auth Error";
    } else if (errorCode == WebViewClient.ERROR_TIMEOUT) {
        message = "The server is taking too much time to communicate. Try again later.";
        title = "Connection Timeout";
    } else if (errorCode == WebViewClient.ERROR_TOO_MANY_REQUESTS) {
        message = "Too many requests during this load";
        title = "Too Many Requests";
    } else if (errorCode == WebViewClient.ERROR_UNKNOWN) {
        message = "Generic error";
        title = "Unknown Error";
    } else if (errorCode == WebViewClient.ERROR_BAD_URL) {
        message = "Check entered URL..";
        title = "Malformed URL";
    } else if (errorCode == WebViewClient.ERROR_CONNECT) {
        message = "Failed to connect to the server";
        title = "Connection";
    } else if (errorCode == WebViewClient.ERROR_FAILED_SSL_HANDSHAKE) {
        message = "Failed to perform SSL handshake";
        title = "SSL Handshake Failed";
    } else if (errorCode == WebViewClient.ERROR_HOST_LOOKUP) {
        message = "Server or proxy hostname lookup failed";
        title = "Host Lookup Error";
    } else if (errorCode == WebViewClient.ERROR_PROXY_AUTHENTICATION) {
        message = "User authentication failed on proxy";
        title = "Proxy Auth Error";
    } else if (errorCode == WebViewClient.ERROR_REDIRECT_LOOP) {
        message = "Too many redirects";
        title = "Redirect Loop Error";
    } else if (errorCode == WebViewClient.ERROR_UNSUPPORTED_AUTH_SCHEME) {
        message = "Unsupported authentication scheme (not basic or digest)";
        title = "Auth Scheme Error";
    } else if (errorCode == WebViewClient.ERROR_UNSUPPORTED_SCHEME) {
        message = "Unsupported URI scheme";
        title = "URI Scheme Error";
    } else if (errorCode == WebViewClient.ERROR_FILE) {
        message = "Generic file error";
        title = "File";
    } else if (errorCode == WebViewClient.ERROR_FILE_NOT_FOUND) {
        message = "File not found";
        title = "File";
    } else if (errorCode == WebViewClient.ERROR_IO) {
        message = "The server failed to communicate. Try again later.";
        title = "IO Error";
    }
     
    if (message != null) {
        new AlertDialog.Builder(mContext)
        .setMessage(message)
        .setTitle(title)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setCancelable(false)
        .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        setResult(RESULT_CANCELED);
                        finish();
                    }
                }).show();
    }       
}

Create your custom view in res/layout folder name it to ic_dialog_alert add textviews for title and error message

Also the Webview be terminated by Resource rendering problem which can be handle by Termination Handling API

eli
  • 8,571
  • 4
  • 30
  • 40