10

Context

In the Android SDK 23 onReceivedError(WebView view, int errorCode, String description, String failingUrl) has been deprecated and replaced with onReceivedError(WebView view, WebResourceRequest request, WebResourceError error). However as per documentation:

Note that unlike the deprecated version of the callback, the new version will be called for any resource (iframe, image, etc), not just for the main page

Problem

We have an app where in the deprecated onReceivedError method there is a code to display a native view instead of letting the user see the error in the WebView.

We would like to replace the deprecated onReceivedError method by the new method. But we don't want to display the native view for errors for any resource, just for the main page.

Question

How can we identify in the new onReceivedError that the error is not from the main page?

PS 1: We would prefer not having a solution like this to store the main url and check it against the failing url.

PS 2: If the solution is to just use the deprecated method, what's the guarantee that it will still be called for new Android versions?

Community
  • 1
  • 1
Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
  • I'm not sure if I got this correctly, ```to store the main url and check it against the failing url.```, why wouldn't you prefer that? Does the main page change or something? – ahasbini May 25 '17 at 22:33
  • yes, it changes as user navigates through the website – Gustavo Pagani May 25 '17 at 22:55
  • Got your point, thought that main page is like the home page of a website. Will your app still be released for pre-Lollipop devices? – ahasbini May 25 '17 at 22:57
  • Sorry, my bad previous comment is unnecessary nvm. Could the main url be differentiated from the resources? Meaning page urls are of form http://www.myurl.com while resource for images are saved in cdn and so are of the form http://www.cdn.com etc...? If so you could use a regular expression and only filter the main urls? That is assuming if you're displaying your website within the app... – ahasbini May 25 '17 at 23:07
  • probably, but we'd rather not going through that path. the previous onReceivedError had a solution out of the box, we wonder if there is an alternative mechanism out of the box out there – Gustavo Pagani May 26 '17 at 08:27

3 Answers3

19

WebResourceRequest has isForMainFrame() method for your scenario which is available starting from API version 21:

enter image description here

Source: https://developer.android.com/reference/android/webkit/WebResourceRequest.html

Sergei B.
  • 3,227
  • 19
  • 18
8

You don't have to store the original URL. You can get it from the WebView passed to the onReceivedError method. It's always the full URL of the current page that the user sees. So, you don't have to worry about them navigating to different pages.

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
    if (request.getUrl().toString().equals(view.getUrl())) {
        notifyError();
    }
}
arsent
  • 6,975
  • 3
  • 32
  • 31
-1

you can use like as codes:

    WebView wv = (WebView) findViewById(R.id.webView);
     wv.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
// here your custom logcat like as share preference or database or static varible.
                super.onReceivedError(view, errorCode, description, failingUrl);
        }
     });

Best of luck!