草庐IT

Android WebView onReceivedError()

coder 2023-06-06 原文

有没有人知道有没有办法在 WebView 中拦截“页面未找到”或“页面未加载错误”?

根据android文档,onReceivedError()应该可以拦截。但我在一个应用程序中测试了它,我故意给出了错误的 URL,它没有做任何事情。

如果 URL 由于任何原因不可用,我希望我的应用能够提供我自己的自定义错误消息。

这是什么都没做的代码:

public void onReceivedError(WebView view, int errorCode,
        String description, String failingUrl) {

    // custom error handling ... show and alert or toast or something
}

最佳答案

根据文档和我的经验,它应该可以正常工作。 您只需在您的 WebView 中使用重写方法 onReceivedError 设置您的 WebClient

这是我的一些旧测试应用程序的 fragment :

 WebView wv = (WebView) findViewById(R.id.webView);
 wv.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
            super.onReceivedError(view, errorCode, description, failingUrl);
    }
 });

我已经测试过了,它工作得很好。检查你的日志,看看你得到什么样的代码错误。 希望对您有所帮助。

关于Android WebView onReceivedError(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4997677/

有关Android WebView onReceivedError()的更多相关文章

随机推荐