android - WebViewClient onReceivedError 已弃用,新版本未检测到所有错误

标签 android webview

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

onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse) 也没有用,因为它只检测到高于 500 的错误,而我得到的是 109 状态码。

是否有一种不被弃用的方法来检测我的 WebView 无法加载?

最佳答案

您还可以执行以下操作:

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

确保您导入 android.annotation.TargetApi

关于android - WebViewClient onReceivedError 已弃用,新版本未检测到所有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32769505/

相关文章:

android - Android 设备上的图像旋转问题

java - 使布局不可点击但子元素可点击

android - 如何阻止广告在我的 Android 应用程序的 Webview 中打开?

react-native - 登录 Instagram 时出错(禁用 Cookie)-React Native WebView

android - 在下拉菜单android上显示图标

android - native 库中的 OpenCV 创建错误

android - 如何将字符串值传递给Webview,例如:传递给youtube”

javascript - Highcharts - 数据不会加载到 Android WebView 上的图表中

android - 可绘制到字节[]

Android webview html 合规性?