在Android SDK 23中onReceivedError(WebView view, int errorCode, String description, String failingUrl)已被弃用并替换为onReceivedError(WebView view, WebResourceRequest request, WebResourceError error).但是,如果我将手机置于飞行模式并在我的WebView上加载网址,则只会调用该方法的弃用版本.
onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse) 也没用,因为它只检测高于500的错误,并且我得到109状态代码.
是否有一种不推荐的方法来检测我的WebView无法加载?
xde*_*s23 126
您还可以执行以下操作:
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// Handle the error
}
@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
Run Code Online (Sandbox Code Playgroud)
确保导入 android.annotation.TargetApi
快乐的编码!
Mik*_*nov 27
请注意,您正在测试的移动设备需要实际运行Android Marshmallow(API 23).即使您在API 23 SDK上开发应用程序,然后在Android Lollipop上运行应用程序,您仍然会获得"旧" onReceivedError,因为它是操作系统的功能,而不是SDK的功能.
此外,"错误代码109"(我猜,这是net::ERR_ADDRESS_UNREACHABLE)不是HTTP错误代码,它是Chrome的错误代码.onReceivedHttpError只调用通过HTTP从服务器收到的错误.当设备处于飞行模式时,它不可能从服务器接收回复.
| 归档时间: |
|
| 查看次数: |
30921 次 |
| 最近记录: |