在Android環(huán)境下WebView中攔截所有請(qǐng)求并替換URL示例詳解
需求背景
接到這樣一個(gè)需求,需要在 WebView 的所有網(wǎng)絡(luò)請(qǐng)求中,在請(qǐng)求的url中,加上一個(gè)xxx=1的標(biāo)志位。
例如 http://www.baidu.com 加上標(biāo)志位就變成了 http://www.baidu.com?xxx=1
尋找解決方案
從 Android API 11 (3.0) 開(kāi)始,WebView 開(kāi)始在 WebViewClient 內(nèi)提供了這樣一條 API ,如下:
public WebResourceResponse shouldInterceptRequest(WebView view, String url)
就是說(shuō)只要實(shí)現(xiàn) WebViewClient 的 shouldInterceptRequest 方法,然后調(diào)用 WebView 的setWebViewClient 就可以了。
但是,在 API21 以上又棄用了上述 API,使用了一條新的 API,如下:
public WebResourceResponse shouldInterceptRequest(WebView view, final WebResourceRequest request)
好吧,為了支持盡量多的版本,看來(lái)兩個(gè)都需要實(shí)現(xiàn)了,發(fā)現(xiàn)一看就非常好用的 String url 變成了一個(gè)WebResourceRequest request。WebResourceRequest 這個(gè)東西是一個(gè)接口,并且是這樣定義的:
public interface WebResourceRequest { Uri getUrl(); boolean isForMainFrame(); boolean hasGesture(); String getMethod(); Map<String, String> getRequestHeaders();}
在其中沒(méi)有發(fā)現(xiàn)任何可以直接替換請(qǐng)求的方法。
然后搜索了一下 Android 代碼中對(duì)他的引用,點(diǎn)我搜索。然后發(fā)現(xiàn) private static class WebResourceRequestImpl implements WebResourceRequest 它的內(nèi)部實(shí)現(xiàn)僅僅是一個(gè)單純的實(shí)體。那這個(gè)東西要替換就非常好辦了,三個(gè)方法都可以做:
動(dòng)態(tài)代理 反射 重新實(shí)現(xiàn)實(shí)現(xiàn)
方案確定了,剩下的就簡(jiǎn)單了。直接上代碼。
首先是往URL字符串加那個(gè)標(biāo)志位的方法
public static String injectIsParams(String url) { if (url != null && !url.contains('xxx=') { if (url.contains('?')) { return url + '&xxx=1'; } else { return url + '?xxx=1'; } } else { return url; }}
然后要攔截所有請(qǐng)求了
webView.setWebViewClient(new WebViewClient() { @SuppressLint('NewApi') @Override public WebResourceResponse shouldInterceptRequest(WebView view, final WebResourceRequest request) { if (request != null && request.getUrl() != null) { String scheme = request.getUrl().getScheme().trim(); if (scheme.equalsIgnoreCase('http') || scheme.equalsIgnoreCase('https')) { return super.shouldInterceptRequest(view, new WebResourceRequest() { @Override public Uri getUrl() { return Uri.parse(injectIsParams(request.getUrl().toString())); } @SuppressLint('NewApi') @Override public boolean isForMainFrame() { return request.isForMainFrame(); } @SuppressLint('NewApi') @Override public boolean hasGesture() { return request.hasGesture(); } @SuppressLint('NewApi') @Override public String getMethod() { return request.getMethod(); } @SuppressLint('NewApi') @Override public Map<String, String> getRequestHeaders() { return request.getRequestHeaders(); } }); } } return super.shouldInterceptRequest(view, request); } @Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) { if (!TextUtils.isEmpty(url) && Uri.parse(url).getScheme() != null) { String scheme = Uri.parse(url).getScheme().trim(); if (scheme.equalsIgnoreCase('http') || scheme.equalsIgnoreCase('https')) { return super.shouldInterceptRequest(view, injectIsParams(url)); } } return super.shouldInterceptRequest(view, url); } });
大功告成。
歡迎指出代碼中的問(wèn)題~~一起學(xué)習(xí)進(jìn)步
注意: 注意保護(hù) URL 的 Scheme,在代碼中特地過(guò)濾了 http 和 https。
引申
上邊的 API 中發(fā)現(xiàn)還能有更多的玩法,比如:
替換 WebResourceResponse,構(gòu)造一個(gè)自己的 WebResourceResponse。比如下列代碼,用一個(gè)包里的本地文件替換掉要請(qǐng)求的網(wǎng)絡(luò)圖片。WebResourceResponse response = null; if (url.contains('logo')) { try { InputStream is = getAssets().open('test.png'); response = new WebResourceResponse('image/png', 'UTF-8', is); } catch (IOException e) { e.printStackTrace(); } }return response;
在 API 21 (5.0) 以上的版本使用了 WebResourceRequest 接口,這個(gè)接口能修改發(fā)出請(qǐng)求的 Header
@Overridepublic Map<String, String> getRequestHeaders() { return request.getRequestHeaders();}
在 API 21 (5.0) 以上的版本中可以區(qū)分 GET 請(qǐng)求和 POST 請(qǐng)求,在某些情況下,需要區(qū)分 AJAX 的不同種類(lèi)請(qǐng)求的時(shí)候可以用到。
到此這篇關(guān)于在Android環(huán)境下WebView中攔截所有請(qǐng)求并替換URL示例詳解的文章就介紹到這了,更多相關(guān)Android WebView攔截所有請(qǐng)求并替換URL內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
