shouldoverrideurlloading和shouldinterceptrequest之间的区别? [英] Difference between shouldoverrideurlloading and shouldinterceptrequest?

查看:164
本文介绍了shouldoverrideurlloading和shouldinterceptrequest之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都请告诉我方法 public WebResourceResponse shouldInterceptRequest(WebView视图,WebResourceRequest请求) public boolean ShouldOverrideUrlLoading(WebView视图,字符串url)之间的区别.

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

我正在创建一个Android应用程序,其中在我的 WebView 中获取一个字符串作为click事件的响应.我想存储此字符串并显示它.我看到了这两种方法.我尝试使用 shouldOverrideUrlLoading ,当我使用google.com作为加载到 WebView 中的url创建一个示例应用程序并单击菜单时,它返回了重定向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?

推荐答案

Android WebKit实现允许开发人员修改 android.webkit.WebSettings 类,例如

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

  • 对JavaScript的支持,
  • 对插件的支持,
  • 文件系统访问
  • 资源检查等

资源检查中,可以通过覆盖 shouldOverrideUrlLoading shouldInterceptRequest 方法来检查对内容和/或资源的请求.

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 在即将打开新页面时被调用,而 shouldInterceptRequest 则在每次加载资源(如css文件,js文件等)时被调用

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.如果用户从WebView内交互式请求资源,则可以通过使用 WebViewClient 类的 shouldOverrideUrlLoading 方法来拦截请求.示例代码如下所示.来源

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;
    }
 }

当即将在当前WebView中加载新URL时,该方法使主机应用程序有机会接管控件.返回值true表示宿主应用程序处理URL,而返回false表示当前WebView处理URL.上面的代码可防止从主机"www.google.com"加载资源.

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".

但是,该方法不会拦截来自内部的资源加载,例如来自HTML或SCRIPT标记内的IFRAME或src属性.此外,XmlHttpRequests也不会被拦截.为了拦截这些请求,您可以使用WebViewClient shouldInterceptRequest 方法.示例代码如下所示.

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);
}

该方法将资源请求通知给主机应用程序,并允许该应用程序返回数据.如果返回值为null,则WebView将照常继续加载资源.否则,将使用返回响应和数据.上面的代码拦截对JavaScript资源(.js)的请求,并返回警报,而不是请求的资源.

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.

更多信息,请参见:WebViewClient shouldOverrideUrlLoading

See more at : WebViewClient shouldOverrideUrlLoading and shouldInterceptRequest

这篇关于shouldoverrideurlloading和shouldinterceptrequest之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆