如何处理来自webView的错误?

时间:2011-11-22 10:52:43

标签: android android-view

我有这段代码:

    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;
        } 
    };

我希望以一种在视图中没有显示任何内容的方式处理错误(404或连接中断时出错)。

怎么做?

1 个答案:

答案 0 :(得分:0)

WebView经常调用应用程序回调(通过WebViewClient和WebChromeClient), 从onReceivedError中实现捕获和定义Thrept 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);
        }

然后使用以下showError方法,您可以了解发生了哪种异常。

    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();
    }       
}

Webview也因资源渲染问题而终止,该问题可由Termination Handling API

处理