0

我有这个代码:

    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 或连接断开时的错误)。

怎么做?

4

1 回答 1

-1

WebView 频繁调用应用回调(通过 WebViewClient 和 WebChromeClient),从 onReceivedError 实现函数来捕获和定义抛出的 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();
    }       
}

res/layout在文件夹名称中创建您的自定义视图以ic_dialog_alert添加标题和错误消息的文本视图

Webview 也被终止处理 API处理的资源渲染问题终止

于 2018-11-10T16:25:08.040 回答