Android WebView onReceivedError() [英] Android WebView onReceivedError()

查看:51
本文介绍了Android WebView onReceivedError()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道有没有办法拦截WebView中的页面未找到"或页面未加载错误"?

Does anyone know if there is a way to intercept a "page not found" or "page not loading error" in WebView?

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

According to the android documentation, onReceivedError() should be able to intercept. but i tested it in an app which I deleberately gave the wrong URL, and it didn't do anything.

如果 URL 因任何原因不可用,我希望我的应用能够给出我自己的自定义错误消息.

I want my app to be able to give my own custom error message if the URL is ever unavailable for any reason.

这是什么都没做的代码:

this is the code that did nothing:

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

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

推荐答案

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

According to documentation and my experience it should work quite fine. You just have to set your WebClient with overriden method onReceivedError in your WebView.

这是我的一些旧测试应用的片段:

Here is the snippet from some of my old test app:

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

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

I've tested it and it works quite fine. Check your logs and see what kind of code error do you get. Hope it helps.

这篇关于Android WebView onReceivedError()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆