Ankush Kapoor
Ankush Kapoor

Reputation: 543

Webview onReceivedError method gets called most of the time

Webview onReceivedError method gets called most of the time in webview setWebViewClient. How can I fix it?

onReceivedError method might be called for several reasons. How can i know if it is called for webpage not available problem only?

        webView = (WebView) findViewById(R.id.webViewOpen);
        webView.loadUrl(url);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setEnableSmoothTransition(true);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                toolbar.setTitle(title);
                try {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        view.loadUrl(request.getUrl().toString());
                    } else {
                        view.loadUrl(url);
                    }
                } catch (Exception e) {
                    Toast.makeText(WebViewOpen.this, "Opening Browser", Toast.LENGTH_SHORT).show();
                    return false;
                }
                return true;
            }

            /*@Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                toolbar.setTitle(title);
                try {
                    webView.loadUrl(url);
                } catch (Exception e) {
                    Toast.makeText(WebViewOpen.this, "Opening Browser", Toast.LENGTH_SHORT).show();
                    return false;
                }
                return true;
            }*/

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                Toast.makeText(getApplicationContext(), "Error! Check your internet connection\nor refresh\n" + error.toString(),
                        Toast.LENGTH_SHORT).show();
                webView.loadUrl("file:///android_asset/dealsoftheday.html");
            }
        });

Upvotes: 1

Views: 2826

Answers (2)

blackorbs
blackorbs

Reputation: 625

Your webpage requested urls different from the main page url so OnReceivedError get called everytime. You just need to check if the requested url is the same as the main page url:

@RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        if(view.getUrl().equals(request.getUrl().toString())){
            // do something like showing custom error page
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        if(view.getUrl().equals(failingUrl)){
            // do something like showing custom error page
        }
    }

Upvotes: 0

yakobom
yakobom

Reputation: 2711

You need to check the error, and handle it as you wish. Something like:

switch (error.getErrorCode()) {
     case WebViewClient.ERROR_BAD_URL:
        ...
        break;
     case WebViewClient.ERROR_CONNECT:
        ...
        break;
     case ...:
        break;
     default:
        break;
}

You can see a full list of the errors here: WebViewClient errors

Upvotes: 2

Related Questions