我試圖集成一個JavaScript插件,它通過使用XmlHttpRequest調用來加載一些資源。我想讓這個腳本在WebView的本地加載頁面中運行。正如你可能已經猜到了,的XmlHttpRequest呼叫不允許本地資源,所以我及時收到以下錯誤:Android WebView:shouldInterceptRequest是否應該由XmlHttpRequest觸發
XMLHttpRequest cannot load file:///android_asset/resources.html. Cross origin requests are only supported for HTTP.
在這一點上,我以爲我可以通過截取電話模擬Web服務器和然後只需加載自己的文件,如:
webView.setWebViewClient(new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest(final WebView view, String url) {
try {
if (url.contains("resources.html")) { //breakpoint here is not triggering
return new WebResourceResponse("text/html", "UTF-8", getAssets().open("resources.html"));
}
} catch (IOException e) {
return super.shouldInterceptRequest(view, url);
}
return super.shouldInterceptRequest(view, url);
}
});
的問題是,shouldInterceptRequest
不會被調用。官方documentation非常簡短,並未指定截取哪種類型的請求。 This article有點暗示該方法確實攔截了XmlHttpRequest調用,但它似乎沒有工作。
有沒有人知道shouldInterceptRequest
應該在之後調用XmlHttpRequest?如果沒有,是否有另一種方法來做到這一點?謝謝
有沒有人知道API <16的替代方案? – AlvaroSantisteban