Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect errors only from the main page in new onReceivedError from WebViewClient

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?

like image 878
Gustavo Avatar asked May 19 '17 10:05

Gustavo


People also ask

What does WebView setWebViewClient new WebViewClient ()) do?

WebViewClient is mainly used to assist WebView in handling various notifications, requests, and other events, which are set by the setWebViewClient method. Called when page resources are loaded, and each resource (such as a picture) is loaded once.

What is the use of WebViewClient in Android?

Android WebView is used to display HTML in an android app. We can use android WebView to load HTML page into android app.

What is WebChromeClient?

WebChromeClient.CustomViewCallback. A callback interface used by the host application to notify the current page that its custom view has been dismissed.

What is shouldOverrideUrlLoading?

shouldOverrideUrlLoading is called when a new page is about to be opened whereas shouldInterceptRequest is called each time a resource is loaded like a css file, a js file etc.


2 Answers

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

like image 130
Sergei B. Avatar answered Oct 20 '22 11:10

Sergei B.


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();
    }
}
like image 31
arsent Avatar answered Oct 20 '22 11:10

arsent