如何在WebView中进行字符编码?(How to do Character Encoding in a WebView?)

编程入门 行业动态 更新时间:2024-10-27 16:23:41
如何在WebView中进行字符编码?(How to do Character Encoding in a WebView?)

我有一个应用程序,其中有几个WebViews,每个显示一段描述文本。 几乎所有的人都在其中出现乱码,通常是单引号或双引号。

例如,一个字符串应该如下所示:

身体'N'空间

结束在WebView中看起来像这样:

身体ÂN空间

这是我第一次尝试:

textWebView.loadData(getHtmlDescription(), "text/html", "UTF-8");

我最终尝试了许多解决方案,包括以下内容:

在loadData之前添加:

WebSettings webSettings = textWebView.getSettings(); webSettings.setDefaultTextEncodingName("UTF-8");

将loadData更改为loadDataWithBaseURL :

WebSettings webSettings = textWebView.getSettings(); webSettings.setDefaultTextEncodingName("UTF-8"); textWebView.loadDataWithBaseURL(null, getHtmlDescription(), "text/html", "UTF-8", null);

将"text/html"改为"text/html; charset=UTF-8" :

WebSettings webSettings = textWebView.getSettings(); webSettings.setDefaultTextEncodingName("UTF-8"); textWebView.loadDataWithBaseURL(null, getHtmlDescription(), "text/html; charset=UTF-8", "UTF-8", null);

不幸的是,这些解决方案中没有任何一个能解决问题

I have an app in which there are several WebViews, each displaying a paragraph of description text. Almost all of them have garbled characters in them, usually where there are single quotes or double quotes.

For example, a string which should look like the following:

Body ‘N’ Space

Ends up looking like this in the WebView:

Body ÂNÂ Space

Here's what I first tried:

textWebView.loadData(getHtmlDescription(), "text/html", "UTF-8");

I ended up trying many solutions, including the following:

Adding this before loadData:

WebSettings webSettings = textWebView.getSettings(); webSettings.setDefaultTextEncodingName("UTF-8");

Changing loadData to loadDataWithBaseURL:

WebSettings webSettings = textWebView.getSettings(); webSettings.setDefaultTextEncodingName("UTF-8"); textWebView.loadDataWithBaseURL(null, getHtmlDescription(), "text/html", "UTF-8", null);

Changing "text/html" to "text/html; charset=UTF-8":

WebSettings webSettings = textWebView.getSettings(); webSettings.setDefaultTextEncodingName("UTF-8"); textWebView.loadDataWithBaseURL(null, getHtmlDescription(), "text/html; charset=UTF-8", "UTF-8", null);

And unfortunately, none of those solutions did the trick!

最满意答案

在尝试了所有这些解决方案之后,我终于试图找到并替换所有可以看到的坏字符。 这太繁琐而且效率低下,但不知何故,我偶然发现了以下两行解决方案,它们完成了所有工作!

在我的getHtmlDescription()方法中,我添加了这两行:

description = description.replace("Â", ""); description = Html.fromHtml(description, Html.FROM_HTML_MODE_LEGACY).toString(); return description;

After trying all those solutions without success, I ended up trying to just find and replace all the bad characters I could see. That was just too cumbersome and inefficient, but somehow I stumbled on the following two-line fix that did the job for all of it!

In my getHtmlDescription() method, I added these two lines:

description = description.replace("Â", ""); description = Html.fromHtml(description, Html.FROM_HTML_MODE_LEGACY).toString(); return description;

更多推荐

本文发布于:2023-07-31 07:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1341875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   如何在   Encoding   WebView   Character

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!