gpt4 book ai didi

安卓 WebView : Should shouldInterceptRequest be triggered by XmlHttpRequest

转载 作者:行者123 更新时间:2023-11-29 00:31:02 26 4
gpt4 key购买 nike

我正在尝试集成一个 javascript 插件,该插件通过使用 XmlHttpRequest 调用来加载一些资源。我希望此脚本在 WebView 的本地加载页面中运行。正如您可能已经猜到的那样,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 WebSettings 中添加了方法 setAllowFileAccessFromFileURLs() 和 setAllowUniversalAccessFromFileURLs()。为 webView 将此设置为 true 可能会解决您的问题。

关于安卓 WebView : Should shouldInterceptRequest be triggered by XmlHttpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15688659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com