如何处理来自 webView 的错误?

发布于 2024-12-17 15:47:57 字数 758 浏览 0 评论 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 或连接中断时的错误)。

怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千笙结 2024-12-24 15:47:57

WebView频繁调用应用程序回调(通过WebViewClient和WebChromeClient),
从 onReceivedError 实现函数来捕获并定义抛出的异常

  @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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文