机器人的WebView onReceivedError() [英] Android WebView onReceivedError()

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

问题描述

有谁知道是否有一种方法拦截找不到网页或web视图页面没有加载错误?

据android的文档, onReceivedError()应该能够拦截。但我在一个应用程序,我deleberately给了错误的URL进行了测试,并没有做任何事情。

我想我的应用程序能够给自己的自定义错误消息,如果网址为任何原因永远无法使用。

这是在code,没有什么做的:

 公共无效onReceivedError(web视图来看,INT错误code,
        字符串描述,字符串failingUrl){

    //自定义错误处理... ...显示和警报或烤面包什么的
}
 

解决方案

据文件和我的经验,它应该工作相当精细。 你只需要设置你的 Web客户端在你的WebView覆盖方法 onReceivedError

下面是我的一些旧的测试应用程序的代码片段:

 的WebView WV =(web视图)findViewById(R.id.webView);
 wv.setWebViewClient(新WebViewClient(){
    @覆盖
    公共无效onReceivedError(web视图来看,INT错误code,字符串描述,字符串failingUrl){
            Log.i(WEB_VIEW_TEST,错误code:+错误code);
            super.onReceivedError(查看,错误code,说明,failingUrl);
    }
 });
 

我测试过它,它工作得很好。检查你的日志,看看你得到什么样的code错误。 希望它能帮助。

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

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.

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
}

解决方案

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.

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

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