Android - Webviewclient onReceivedHttpError确定是否是主要资源

时间:2017-04-27 17:23:05

标签: android webview android-webview http-error webviewclient

WebViewClient.onReceivedError已被弃用,现在我必须使用onReceivedHttpError来处理webview错误,但是此方法会从任何资源收到错误,这不是我想要的。

我希望只检测主网址失败

如果我使用的是 API 10 ,我该如何检测错误?

我尝试使用request.getUrl()方法,但它只与API 23兼容。

1 个答案:

答案 0 :(得分:2)

与此同时,我最终这样做了:

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                //your thing (f.e. show error message)
            }

            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                   //url is the original loading url
                    if(request.getUrl().equals(url)) { 
                        //your thing (f.e. show error message)
                    }
                }

            }