Search code examples
androidwebviewandroid-webviewhttp-errorwebviewclient

Android - Webviewclient onReceivedHttpError determine if is main resource


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.


Solution

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