我有一个应用程序,其中有几个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;更多推荐
发布评论