WebView 中的字符集 (Android)

发布于 2024-11-09 19:18:47 字数 689 浏览 0 评论 0原文

我正在尝试使用 loadData() 方法在 WebView 中显示 Java 字符串中的一些数据:

wv.loadData(myString, "text/html, "utf-8");

我遇到的问题是 WebView 破坏了非 ASCII(我假设?)字符的输出。如果我使用 TextView 而不是 WebView,则不会出现此问题,并且文本会正确显示(尽管涉及一些 HTML 标记,因此 TextView 并不是最终所需要的)。

如果有帮助,当我运行以下代码时:

for(int i = 0; i < myString.length() && i < 400; i++)
      Log.i("Text", myString.charAt(i) + ": " + (int) myString.charAt(i));

日志中出现违规字符:

05-27 13:15:45.110: INFO/Text(606): â: 8217

我认为这是一个字符集问题,但我不太确定如何解决它。

以下是 HTML 片段(我不确定我的雇主是否允许完整发布内容):

突尼斯总理的

性格引起了问题。注意:我显示的不是一个完整的 HTML 页面.. 只是带有 HTML 标记的文本,如果这很重要的话..

I am trying to display some data from a Java String in a WebView, using the loadData() method:

wv.loadData(myString, "text/html, "utf-8");

The issue I am having is that the WebView mangles the output of non-ASCII (I assume?) characters. If I use a TextView instead of a WebView, this problem does not occur, and the text displays correctly (although there is some HTML markup involved, so a TextView is not ultimately desirable).

If it helps, when I run the following code:

for(int i = 0; i < myString.length() && i < 400; i++)
      Log.i("Text", myString.charAt(i) + ": " + (int) myString.charAt(i));

an offending character appears as such in the log:

05-27 13:15:45.110: INFO/Text(606): â: 8217

This is a character set issue, I think, but I'm not quite sure how to resolve it.

Here's a snippet of the HTML (I'm not sure if my employer would allow a full posting of the content):

Tunisia’s prime minister

It is the ’ character that is causing issue. NB: What I'm displaying is not a fully-formed HTML page.. just text with HTML markup, if that matters..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

自由范儿 2024-11-16 19:18:47

我遇到了类似的问题,我传递给 loadData() 方法的字符串没有在 WebView 中正确显示(有奇怪的字符而不是正确的 DBCS 字符)。就我而言,字符串已正确编码为 UTF-8,问题在于 loadData() 如何使用该字符串。

我发现在 mime 类型参数中指定字符集非常重要,而不是编码参数。具体来说,调用必须是

wv.loadData(myString, "text/html; charset=utf-8", encoding);

在我的例子中,我传递了 null 作为编码变量并且它起作用了(Android 4.2.1)。阅读有关 loadData() 的 API 文档;它似乎表明它将接受“base64”,并且所有其他值都会导致数据参数被视为 URL 编码的 ASCII。

因此,您不能使用encoding参数设置字符串的编码,您必须在mime类型中指定编码。

I had a similar problem, where the string I was passing into the loadData() method was not being presented properly in the WebView (had odd characters instead of correct DBCS ones). In my case, the string was properly encoded in UTF-8 and the problem was in how loadData() was using the string.

I found that is important to specify the character set within the mime type argument, and not the encoding argument. Specifically the call had to be

wv.loadData(myString, "text/html; charset=utf-8", encoding);

In my case, I passed null as the encoding variable and it worked (Android 4.2.1). Reading the API documentation on loadData(); it seems to indicate that it will accept "base64" and all other values will cause the data argument to be treated as URL-encoded ASCII.

Therefore, you CANNOT set the encoding of the string with the encoding parameter, you must specify the encoding in the mime type.

痴骨ら 2024-11-16 19:18:47

尝试:

wv.loadData(URLEncoder.encode(myString).replace("+", "%20"), "text/html, "utf-8");

或者,如果这不起作用:

wv.loadData(myString, "text/html, "ISO-8859-1");

或者两者的组合。其中之一通常对我有用。

Try:

wv.loadData(URLEncoder.encode(myString).replace("+", "%20"), "text/html, "utf-8");

Or, if that doesn't work:

wv.loadData(myString, "text/html, "ISO-8859-1");

Or a combination of the two. One of those generally works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文