不推荐使用WebViewClient onReceivedError,新版本无法检测所有错误 [英] WebViewClient onReceivedError deprecated, new version does not detect all errors

查看:4854
本文介绍了不推荐使用WebViewClient onReceivedError,新版本无法检测所有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android SDK 23中,onReceivedError(WebView view, int errorCode, String description, String failingUrl)已被弃用,并替换为onReceivedError(WebView view, WebResourceRequest request, WebResourceError error).但是,如果我将手机置于飞行模式并在WebView上加载网址,则只会调用该方法的不建议使用的版本.

In the Android SDK 23 onReceivedError(WebView view, int errorCode, String description, String failingUrl) has been deprecated and replaced with onReceivedError(WebView view, WebResourceRequest request, WebResourceError error). However if I put my phone in Airplane mode and load an url on my WebView, only the deprecated version of the method is called.

onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse)也不有用,因为它只能检测到高于500的错误,并且我得到的状态码为109.

onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse) is also not useful, as it only detects errors higher than 500, and I am getting a 109 status code.

是否有一种不建议使用的方法来检测我的WebView加载失败?

Is there a non-deprecated way of detecting that my WebView failed to load?

推荐答案

请注意,您要测试的移动设备需要实际运行Android Marshmallow(API 23).即使您使用API​​ 23 SDK开发应用程序,然后在Android Lollipop上运行该应用程序,您仍然会得到旧的" onReceivedError,因为它是操作系统的功能,而不是SDK的功能.

Please note that the mobile device where you are testing needs to actually run Android Marshmallow (API 23). Even if you develop your app on API 23 SDK, but then run the app on Android Lollipop, you will still be getting the "old" onReceivedError, because it's the feature of the OS, not of an SDK.

此外,错误代码109"(我想这是net::ERR_ADDRESS_UNREACHABLE)也不是HTTP错误代码,而是Chrome的错误代码.仅对通过HTTP从服务器收到的错误调用onReceivedHttpError.当设备处于飞行模式时,它可能无法收到来自服务器的回复.

Also, the "error code 109" (I guess, this is net::ERR_ADDRESS_UNREACHABLE) is not an HTTP error code, it's Chrome's error code. onReceivedHttpError is only called for the errors received from the server via HTTP. When the device is in airplane mode, it can't possibly receive a reply from a server.

这篇关于不推荐使用WebViewClient onReceivedError,新版本无法检测所有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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