shouldInterceptRequest、shouldOverrideUrlLoading区别

h5页面请求如.js等资源文件的时调用,可以截取并更换资源文件(用native资源替换h5页面的资源)。
一个h5页面可能会有多个资源文件请求。
public WebResourceResponse shouldInterceptRequest(WebView view,WebResourceRequest request)
 
 
当前webview要加载下一个h5页面时调用,可以截取并通过return值确定是否要本地处理或者交给webview处理
 
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
 


 
 

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.


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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值