WebView 的 loadData 的编码问题
我正在 WebView 中使用“
String uri = Uri.encode(html);
webview.loadData(uri, "text/html", "ISO-8859-1");
显示时,latin1 字符被奇怪的字符替换”来加载一些包含 latin-1 字符的数据。
如果我直接在 TextView 中加载 html(只是为了测试),拉丁字符会正确显示。
有人可以帮忙吗?
谢谢
html:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- some html -->
</html>
I'm loading some data, containing latin-1 characters, in a WebView using
String uri = Uri.encode(html);
webview.loadData(uri, "text/html", "ISO-8859-1");
When displayed, the latin1 characters are replaced by weird characters.
If I load the html directly in a TextView (just to test), latin characters are properly displayed.
Anybody can help?
Thanks
html:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- some html -->
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这可以完美地工作,特别是在 Android 4.0 上,它显然忽略了 HTML 内部的字符编码。
在 2.3 和 4.0.3 上测试。
事实上,我不知道除了“base64”之外,最后一个参数还采用什么其他值。一些 Google 示例将 null 放在那里。
您应该始终使用 UTF-8 编码。所有其他字符编码已经过时很多年了。
This works flawlessly, especially on Android 4.0, which apparently ignores character encoding inside HTML.
Tested on 2.3 and 4.0.3.
In fact, I have no idea about what other values besides "base64" does the last parameter take. Some Google examples put null in there.
You should always use UTF-8 encoding. Every other character encoding has become obsolete for many years already.
使其正常工作的唯一方法,如此处所述:
无 URI 编码,utf -8...加载数据错误?
Only way to have it working, as commented here:
No URI encoding, utf-8... loadData bug?
问题的解决方案之一。
One of solution of problem.
我显示了 © 2011,它显示的是 ©。
使用下面的代码我已经实现了显示正确的值©2011
I have display © 2011 and it was displaying ©.
With the below code i have achieved displaying correct value © 2011
据我所知:
首先,loadData()方法用于加载原始html代码。
其次,只需将 html 代码直接放入 loadData() 中,不要对其进行编码
您可能想这样尝试:
干杯!
AFAIK that:
Firstly, loadData() method is used to load raw html code.
Secondly, just put the html code directly to the loadData(), don't encode it
You might wanna try like this:
Cheers!
我也遇到过这样的问题:到处都有一个奇怪的角色。尝试了不同的选项,但有效的选项如下。
wv_news_text 是 Web 视图。
I too had the problem of getting a weird character like  here and there. Tried different options, but the one that worked is below.
wv_news_text is the WebView.
以下代码对我有用。
Following code worked for me.