Android shouldoverrideurlloading 和 shouldinterceptrequest 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26651586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between shouldoverrideurlloading and shouldinterceptrequest?
提问by Nevaeh
Anyone please tell me the difference between methods public WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request)
and public boolean shouldOverrideUrlLoading(WebView view, String url)
.
任何人请告诉我方法public WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request)
和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.
我正在创建一个 android 应用程序,其中一个字符串作为我的单击事件的响应WebView
。我想存储这个字符串并显示它。我看到了这两种方法。我尝试使用shouldOverrideUrlLoading
which 返回重定向 url 当我检查使用 google.com 作为我加载到我的WebView
并单击菜单中的 url 创建示例应用程序。
Could anyone please tell me the difference between both methods and which one should i use?
谁能告诉我这两种方法之间的区别,我应该使用哪一种?
回答by Giru Bhai
The Android WebKit implementation allows the developer to modify a WebViewthrough the android.webkit.WebSettingsclass such as
Android WebKit 实现允许开发者通过android.webkit.WebSettings类修改WebView,例如
- Support for JavaScript,
- Support for Plugins,
- File System Access,
- Resource Inspection etc.
- 支持 JavaScript,
- 支持插件,
- 文件系统访问,
- 资源检查等
In Resource Inspection, it is possible to inspect the requests for content and/or resources by overriding shouldOverrideUrlLoadingand shouldInterceptRequestmethods.
在Resource Inspection 中,可以通过覆盖shouldOverrideUrlLoading和shouldInterceptRequest方法来检查对内容和/或资源的请求。
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.
1.shouldOverrideUrlLoading
在新页面即将打开时shouldInterceptRequest
调用,而在每次加载资源(如 css 文件、js 文件等)时调用。
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
2.如果用户从WebView内部交互请求资源,可以通过使用类的shouldOverrideUrlLoading
方法WebViewClient
来拦截请求。示例代码如下所示。 来源
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”.
当一个新的 URL 即将被加载到当前的 WebView 中时,该方法使宿主应用程序有机会接管控制。返回值 true 表示宿主应用程序处理 URL,而 return false 表示当前 WebView 处理 URL。上面的代码阻止从主机“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.
但是,该方法不会拦截从内部加载的资源,例如来自 HTML 或 SCRIPT 标签内的 IFRAME 或 src 属性。此外 XmlHttpRequests 也不会被拦截。为了拦截这些请求,您可以使用 WebViewClientshouldInterceptRequest
方法。示例代码如下所示。
@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.
该方法将资源请求通知主机应用程序并允许应用程序返回数据。如果返回值为空,WebView 将继续像往常一样加载资源。否则,将使用返回响应和数据。上面的代码拦截对 JavaScript 资源 (.js) 的请求并返回警报而不是请求的资源。
See more at : WebViewClient shouldOverrideUrlLoadingand shouldInterceptRequest
查看更多信息:WebViewClient shouldOverrideUrlLoading和shouldInterceptRequest
回答by Tom
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 时会调用 shouldOverrideUrlLoading ,例如,当你做你的初始:
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.
您的 shouldOverrideUrlLoading 将被调用,如果用户单击链接浏览到新页面,它将再次被调用。
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).
应该为当前页面内的所有请求调用 shouldInterceptRequest,例如。当我 HTML 导入字体时,我看到 shouldInterceptRequest 被调用,或者当 webView 尝试在我的页面上加载图像时,它被调用(但我没有看到它为 ajax 请求调用,所以我仍然有点困惑)。