WebView 的 loadData 的编码问题

发布于 2024-12-07 18:52:46 字数 573 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

属性 2024-12-14 18:52:46
myWebView.loadData(myHtmlString, "text/html; charset=UTF-8", null);

这可以完美地工作,特别是在 Android 4.0 上,它显然忽略了 HTML 内部的字符编码。

在 2.3 和 4.0.3 上测试。

事实上,我不知道除了“base64”之外,最后一个参数还采用什么其他值。一些 Google 示例将 null 放在那里。

您应该始终使用 UTF-8 编码。所有其他字符编码已经过时很多年了。

myWebView.loadData(myHtmlString, "text/html; charset=UTF-8", null);

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.

森林很绿却致人迷途 2024-12-14 18:52:46

使其正常工作的唯一方法,如此处所述:

webview.loadDataWithBaseURL("fake://not/needed", html, "text/html", "utf-8", "");

无 URI 编码,utf -8...加载数据错误?

Only way to have it working, as commented here:

webview.loadDataWithBaseURL("fake://not/needed", html, "text/html", "utf-8", "");

No URI encoding, utf-8... loadData bug?

青朷 2024-12-14 18:52:46
String start = "<html><head><meta http-equiv='Content-Type' content='text/html' charset='UTF-8' /></head><body>";
String end = "</body></html>";

webcontent.loadData(start+ YOURCONTENT + end, "text/html; charset=UTF-8", null);

问题的解决方案之一。

String start = "<html><head><meta http-equiv='Content-Type' content='text/html' charset='UTF-8' /></head><body>";
String end = "</body></html>";

webcontent.loadData(start+ YOURCONTENT + end, "text/html; charset=UTF-8", null);

One of solution of problem.

旧街凉风 2024-12-14 18:52:46

我显示了 © 2011,它显示的是 ©。

使用下面的代码我已经实现了显示正确的值©2011

webViewContent.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);

I have display © 2011 and it was displaying ©.

With the below code i have achieved displaying correct value © 2011

webViewContent.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
美男兮 2024-12-14 18:52:46
webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null); 
webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null); 
任性一次 2024-12-14 18:52:46

据我所知:
首先,loadData()方法用于加载原始html代码。
其次,只需将 html 代码直接放入 loadData() 中,不要对其进行编码

您可能想这样尝试:

webview.loadData(uri, "text/html", "ISO-8859-1");

干杯!

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:

webview.loadData(uri, "text/html", "ISO-8859-1");

Cheers!

删除会话 2024-12-14 18:52:46

我也遇到过这样的问题:到处都有一个奇怪的角色。尝试了不同的选项,但有效的选项如下。

String style_sheet_url = "http://something.com/assets/css/layout.css";
    String head = "<head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" + 
            "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + style_sheet_url + "\" /></head>";    
    String locdata = "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + head + "<body>"+ data + "</body></html>";
    wv_news_text.loadData(locdata, "text/html", "utf-8");

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.

String style_sheet_url = "http://something.com/assets/css/layout.css";
    String head = "<head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" + 
            "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + style_sheet_url + "\" /></head>";    
    String locdata = "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + head + "<body>"+ data + "</body></html>";
    wv_news_text.loadData(locdata, "text/html", "utf-8");

wv_news_text is the WebView.

梦里泪两行 2024-12-14 18:52:46

来自 Java 文档的有关 loadData 方法的信息

使用“数据”方案 URL 将给定数据加载到此 WebView 中。

请注意,JavaScript 的同源策略意味着脚本运行在
使用此方法加载的页面将无法访问内容
使用“data”以外的任何方案加载,包括“http(s)”。到
避免此限制,使用 loadDataWithBaseURL() 和适当的
基本网址。

编码参数指定数据是base64还是URL
编码。如果数据是base64编码的,则编码的值
参数必须是“base64”。对于参数的所有其他值,
包括null,假设数据使用ASCII编码
安全 URL 字符范围内的八位字节并使用标准
%xx 超出该范围的八位字节 URL 的十六进制编码。例如,
'#'、'%'、'\'、'?'应替换为 %23、%25、%27、%3f
分别。

此方法形成的“数据”方案 URL 使用默认的 US-ASCII
字符集。如果您需要设置不同的字符集,您应该形成
一个'data'方案URL,它明确指定了一个字符集参数
URL 的媒体类型部分并调用 loadUrl(String)。
请注意,从数据的媒体类型部分获取的字符集
URL 始终覆盖 HTML 或 XML 文档中指定的 URL
本身。

以下代码对我有用。

String base64EncodedString = null;
                        try {
                            base64EncodedString = android.util.Base64.encodeToString((preString+mailContent.getBody()+postString).getBytes("UTF-8"), android.util.Base64.DEFAULT);
                        } catch (UnsupportedEncodingException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        if(base64EncodedString != null)
                        {
                            wvMailContent.loadData(base64EncodedString, "text/html; charset=utf-8", "base64");  
                        }
                        else
                        {
                            wvMailContent.loadData(preString+mailContent.getBody()+postString, "text/html; charset=utf-8", "utf-8");

Info from Java docs about loadData method

Loads the given data into this WebView using a 'data' scheme URL.

Note that JavaScript's same origin policy means that script running in
a page loaded using this method will be unable to access content
loaded using any scheme other than 'data', including 'http(s)'. To
avoid this restriction, use loadDataWithBaseURL() with an appropriate
base URL.

The encoding parameter specifies whether the data is base64 or URL
encoded. If the data is base64 encoded, the value of the encoding
parameter must be 'base64'. For all other values of the parameter,
including null, it is assumed that the data uses ASCII encoding for
octets inside the range of safe URL characters and use the standard
%xx hex encoding of URLs for octets outside that range. For example,
'#', '%', '\', '?' should be replaced by %23, %25, %27, %3f
respectively.

The 'data' scheme URL formed by this method uses the default US-ASCII
charset. If you need need to set a different charset, you should form
a 'data' scheme URL which explicitly specifies a charset parameter in
the mediatype portion of the URL and call loadUrl(String) instead.
Note that the charset obtained from the mediatype portion of a data
URL always overrides that specified in the HTML or XML document
itself.

Following code worked for me.

String base64EncodedString = null;
                        try {
                            base64EncodedString = android.util.Base64.encodeToString((preString+mailContent.getBody()+postString).getBytes("UTF-8"), android.util.Base64.DEFAULT);
                        } catch (UnsupportedEncodingException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        if(base64EncodedString != null)
                        {
                            wvMailContent.loadData(base64EncodedString, "text/html; charset=utf-8", "base64");  
                        }
                        else
                        {
                            wvMailContent.loadData(preString+mailContent.getBody()+postString, "text/html; charset=utf-8", "utf-8");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文