【发布时间】:2014-09-11 17:15:05
【问题描述】:
我正在尝试集成一个 javascript 插件,该插件通过使用 XmlHttpRequest 调用来加载一些资源。我希望这个脚本在 WebView 的本地加载页面中运行。正如您可能已经猜到的那样,本地资源不允许 XmlHttpRequest 调用,所以我立即收到以下错误:
XMLHttpRequest 无法加载 file:///android_asset/resources.html。叉 源请求仅支持 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 之后调用?如果没有,还有其他方法可以做到这一点吗?谢谢
【问题讨论】:
标签: android webview xmlhttprequest