在 Android 3.1 的 WebView 中加载 URL 在设备上失败,但在模拟器上有效
我正在尝试将以下 URL 加载到 webview 中:index.html?name
这在 Android 2.2 设备和模拟器以及 Android 3.1 模拟器上运行良好。但是,当我在 Android 3.1 Galaxy 选项卡上加载该应用程序时,遇到了错误:
网页不可用
位于 file:///android_asset/index.html?name 的网页可能暂时无法访问,或者可能已永久移至新网址。
似乎它没有注册它是本地资产,并且正在尝试访问它的网络。知道为什么会发生这种情况,而且只发生在物理 3.1 设备上吗?如果我尝试仅加载index.html,那么它对所有内容都可以正常工作。我对此表示怀疑,但这可能与问号有关吗?
提前致谢。
I'm trying to load the following URL into a webview: index.html?name
This works fine on Android 2.2 device and emulator, as well as Android 3.1 emulator. However, when I load the app up on my Android 3.1 galaxy tab, I am met with an error:
Webpage not available
The webpage at file:///android_asset/index.html?name might be temporarily down or it may have moved permanently to a new web address.
Seems like it's not registering that it's a local asset and is trying to access the web for it. Any idea why this is happening, and only on the physical 3.1 device? If I try to load just index.html then it works fine on everything. I doubt it, but could it have something to do with the question mark?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,但有一个解决方法...
将 ?name 更改为 #name 即可,因为 Android 支持命名锚点。然后,您可以使用如下 JavaScript 来检测锚点并对其采取行动:
I had the same issue but there's a workaround...
Change ?name to #name instead and it will work, since named anchors are supported by Android. You can then use JavaScript like the following to detect the anchor and act upon it:
我想你已经回答了你自己的问题。通过 HTTP,“?”在 URL 中用于将 URL 参数与主 URL 分开;虽然有趣的是,这可以在模拟器中与 file:///android_asset 一起使用,但我不愿意称其为错误。
如果您需要将参数传递给 HTML 页面(可能供 javascript 使用),一种选择是使用锚语法(例如
file:///android_asset/index.html#name )
I think you've answered your own question. Over HTTP, the '?' in a URL is used to separate URL parameters from the main URL; although it's interesting that this works with file:///android_asset in the emulators, I'd hesitate to call it a bug.
If you need to pass arguments to the HTML page (perhaps for javascript to use), one option would be to use anchor syntax (e.g.
file:///android_asset/index.html#name
)根据标准,
file://
URL 不支持查询字符串,例如?name
。所以这很难说是一个错误。您首先想要实现什么目标?您的代码的哪一部分应该处理该名称?如果是 JavaScript,则加载页面,然后通过另一个带有javascript:
URL 的loadUrl()
调用来调用页面上的一些 JavaScript。As per the standard,
file://
URLs don't support query strings, like your?name
. So this is hardly a bug. What are you trying to accomplish in the first place? Which part of your code is supposed to process the name? If JavaScript, then load the page, then invoke some JavaScript on it via anotherloadUrl()
call with ajavascript:
URL.