android - Difference between shouldoverrideurlloading and shouldinterceptrequest? -
android - Difference between shouldoverrideurlloading and shouldinterceptrequest? -
anyone please tell me difference between methods public webresourceresponse shouldinterceptrequest (webview view, webresourcerequest request)
, public boolean shouldoverrideurlloading(webview view, string url)
.
i'm creating android application in string got response of click event in webview
.i want store string , display it.i saw both of these methods.i tried using shouldoverrideurlloading
returns redirect url when checked creating sample app using google.com url loaded in webview
, clicked menu.
could please tell me difference between both methods , 1 should use?
the android webkit implementation allows developer modify webview through android.webkit.websettings class such
support javascript, support plugins, file scheme access, resource inspection etc.in resource inspection, possible inspect requests content and/or resources overriding shouldoverrideurlloading , shouldinterceptrequest methods.
but above 2 methods utilize different purpose such as
1.shouldoverrideurlloading
called when new page opened whereas shouldinterceptrequest
called each time resource loaded css file, js file etc.
2.if user interactively requests resource within webview possible through utilize of shouldoverrideurlloading
method of webviewclient
class intercept request. illustration code 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")) { homecoming true; } homecoming false; } }
the method gives host application chance take on command when new url loaded in current webview. homecoming value of true means host application handles url, while homecoming false means current webview handles url. code above prevents resources beingness loaded host “www.google.com”.
however, method not intercept resource loading within, such iframe or src attribute within html or script tag example. additionally xmlhttprequests not intercepted. in order intercept these requests can create utilize of webviewclient shouldinterceptrequest
method. illustration code presented below.
@override public webresourceresponse shouldinterceptrequest(final webview view, string url) { if (url.contains(".js")) { homecoming getwebresourceresponsefromstring(); } else { homecoming super.shouldinterceptrequest(view, url); } } private webresourceresponse getwebresourceresponsefromstring() { homecoming getutf8encodedwebresourceresponse(new stringbufferinputstream("alert('!no!')")); } private webresourceresponse getutf8encodedwebresourceresponse(inputstream data) { homecoming new webresourceresponse("text/css", "utf-8", data); }
the method notifies host application of resource request , allows application homecoming data. if homecoming value null, webview go on load resource usual. otherwise, homecoming response , info used. code above intercepts requests javascript resources (.js) , returns alert instead of requested resource.
see more @ : webviewclient shouldoverrideurlloading , shouldinterceptrequest
android webview webclient intercept
Comments
Post a Comment