Android - Webviewclient onReceivedHttpError determine if is main resource

被刻印的时光 ゝ 提交于 2019-12-24 09:58:45

问题


WebViewClient.onReceivedError was deprecated, now I have to use onReceivedHttpError to handle the webview errors, however this method receives the error from any resource, which is not what I desire.

I wish only to detect the main URL failing.

How should I detect the error if I am using API 10?

I tried using the request.getUrl() method but it is only compatible with API 23.


回答1:


Meanwhile, I ended up doing this:

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

            }


来源:https://stackoverflow.com/questions/43664260/android-webviewclient-onreceivedhttperror-determine-if-is-main-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!