5

I am working on a solution where I am loading a url. I need to get Http Error so that I can reload the url to create a new session.

@Override
   public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);
            Toast.makeText(getActivity(), "Unexpected error occurred.Reload page again.", Toast.LENGTH_SHORT).show();
            if(errorResponse.getStatusCode() == 401){
                customWebView.loadUrl(url);
            }
        }

But the problem with above code is that it work from Marshmallow. Can any one suggest me a solution where I can get error below android version < 23?

ABHI
  • 121
  • 2
  • 6

1 Answers1

2

onReceivedHttpError

void onReceivedHttpError (WebView view, 
                WebResourceRequest request, 
                WebResourceResponse errorResponse)

Notify the host application that an HTTP error has been received from the server while loading a resource.

Can any one suggest me a solution where I can get error below android version < 23

onReceivedHttpError Added in API level 23. So it will not work below 23.

Try with

.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {

        /*if(Build.VERSION.SDK_INT<23)
         {
            // You work
         }*/
        }

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

        /*if(Build.VERSION.SDK_INT >= 23)
            {
                 // Your Work
            }*/
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler,
                                       SslError error) {
        }
    });

FYI

onReceivedError

This method was deprecated in API level 23. Use onReceivedError(WebView, WebResourceRequest, WebResourceError) instead.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198