Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between shouldoverrideurlloading and shouldinterceptrequest?

Anyone please tell me the difference between methods public WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request) and public boolean shouldOverrideUrlLoading(WebView view, String url).

I'm creating an android application in which a string is got as the response of a click event in my WebView.I want to store this string and display it.I saw both of these methods.I tried using shouldOverrideUrlLoading which returns the redirect url when i checked with creating a sample app using google.com as the url which i loaded in my WebView and clicked a menu.

Could anyone please tell me the difference between both methods and which one should i use?

like image 213
Nevaeh Avatar asked Oct 30 '14 11:10

Nevaeh


People also ask

What is a WebViewClient?

WebViewClient is the object responsible for most the actions inside a WebView. JavaScript enabled, security, routing, etc. You can make a custom one, as well as use a Chrome one.

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.


2 Answers

The Android WebKit implementation allows the developer to modify a WebView through the android.webkit.WebSettings class such as

  • Support for JavaScript,
  • Support for Plugins,
  • File System Access,
  • Resource Inspection etc.

In Resource Inspection, it is possible to inspect the requests for content and/or resources by overriding shouldOverrideUrlLoading and shouldInterceptRequest methods.

But above two methods are use for different purpose such as

1.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.If a user interactively requests a resource from within a WebView it is possible through the use of the shouldOverrideUrlLoading method of the WebViewClient class to intercept the request. Example code is presented below. Source

 private class MyWebViewClient extends WebViewClient {      @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {         if (Uri.parse(url).getHost().equals("www.google.com")) {             return true;         }         return false;     }  } 

The method gives the host application a chance to take over the control when a new URL is about to be loaded in the current WebView. A return value of true means the host application handles the URL, while return false means the current WebView handles the URL. The code above prevents resources from being loaded from the host “www.google.com”.

However, the method does not intercept resource loading from within, such as from an IFRAME or src attribute within an HTML or SCRIPT tag for example. Additionally XmlHttpRequests would also not be intercepted. In order to intercept these requests you can make use of the WebViewClient shouldInterceptRequest method. Example code is presented below.

@Override public WebResourceResponse shouldInterceptRequest(final WebView view, String url) {     if (url.contains(".js")) {         return getWebResourceResponseFromString();     } else {         return super.shouldInterceptRequest(view, url);     } } private WebResourceResponse getWebResourceResponseFromString() {     return getUtf8EncodedWebResourceResponse(new StringBufferInputStream("alert('!NO!')")); } private WebResourceResponse getUtf8EncodedWebResourceResponse(InputStream data) {     return new WebResourceResponse("text/javascript", "UTF-8", data); } 

The method notifies the host application of a resource request and allows the application to return the data. If the return value is null, the WebView will continue to load the resource as usual. Otherwise, the return response and data will be used. The code above intercepts requests for JavaScript resources (.js) and returns an alert instead of the requested resource.

See more at : WebViewClient shouldOverrideUrlLoading and shouldInterceptRequest

like image 74
Giru Bhai Avatar answered Sep 23 '22 23:09

Giru Bhai


I believe that shouldOverrideUrlLoading is invoked when a new page is being loaded into the webview, so for example, when you do your initial:

webview.loadUrl( "file:///android_asset/web/index.html" );       

YOur shouldOverrideUrlLoading will get invoked, and it will get invoked again if user clicks on a link to browse to a new page.

shouldInterceptRequest should get called for all requests made within the current page, eg. when I HTML import fonts I see shouldInterceptRequest getting called, or when the webView tries to load images on my page it gets called (but I'm not seeing it called for ajax requests, so I'm still a bit confused).

like image 24
Tom Avatar answered Sep 20 '22 23:09

Tom