Android WebView UTF-8 未显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3312643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android WebView UTF-8 not showing
提问by Ian Vink
I have a webview and am trying to load simple UTF-8 text into it.
我有一个 webview,正在尝试将简单的 UTF-8 文本加载到其中。
mWebView.loadData("將賦予他們的傳教工作標示為", "text/html", "UTF-8");
But the WebView displays ANSI/ASCII garbage.
但 WebView 显示 ANSI/ASCII 垃圾。
Obviously an encoding issue, but what am I missing in telling the webview to display the Unicode text?
显然是编码问题,但是我在告诉 webview 显示 Unicode 文本时缺少什么?
This is a HelloWorld app.
这是一个 HelloWorld 应用程序。
回答by Jorgesys
Use:
用:
mWebView.loadDataWithBaseURL(null, "將賦予他們的傳教工作標示為", "text/html", "utf-8", null);
or using WebSettings with setDefaultTextEncoding:
或使用带有setDefaultTextEncoding 的WebSettings :
WebSettings settings = mWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");
For recent versions of Android, API 16 to 22 it was tested and work properly using loadData() method, requires the mimeType to include: "charset=utf-8".
对于最新版本的 Android,API 16 到 22 已经过测试并使用 loadData() 方法正常工作,需要 mimeType 包括:“charset=utf-8”。
WebView mWebView = (WebView) findViewById(R.id.myWebView);
WebSettings settings = mWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");
mWebView.loadData(myCharacters, "text/html; charset=utf-8",null);
or
或者
mWebView.loadData(myCharacters, "text/html; charset=utf-8","UTF-8");
回答by Cameron Lowell Palmer
This problem goes back to at least Gingerbread
这个问题至少可以追溯到 Gingerbread
This seems to have been broken in some form or fashion forever. Issue 1733
这似乎以某种形式或时尚永远被打破了。 第 1733 期
Use loadDataWithBaseURL instead of loadData
使用 loadDataWithBaseURL 而不是 loadData
// Pretend this is an html document with those three characters
String scandinavianCharacters = "???";
// Won't render correctly
webView.loadData(scandinavianCharacters, "text/html", "UTF-8");
// Will render correctly
webView.loadDataWithBaseURL(null, scandinavianCharacters, "text/html", "UTF-8", null);
Now the part that is truly annoying is that on the Samsung Galaxy S II (4.0.3) loadData() works just fine, but testing on the Galaxy Nexus (4.0.2) the multi-byte characters are garbled unless you use loadDataWithBaseURL(). WebView Documentation
现在真正令人讨厌的部分是在三星 Galaxy S II (4.0.3) 上 loadData() 工作得很好,但在 Galaxy Nexus (4.0.2) 上测试多字节字符是乱码,除非你使用 loadDataWithBaseURL( )。WebView 文档
Recent versions of Android
最近的 Android 版本
Some are reporting a change in the behavior of the loadData calls requiring the mimeType
to include charset=utf-8
.
有些人报告需要mimeType
包含的 loadData 调用的行为发生了变化charset=utf-8
。
webView.loadData(scandinavianCharacters, "text/html; charset=utf-8", "UTF-8");
Discussion
讨论
The first time I saw this my boss brought me his phone, an early Nexus, while I was developing at the time on a Samsung Galaxy II and it showed up in our economic news feed on his phone which had a lot of non-ASCII characters. So, not only is this a long standing issue within Android, but it also isn't consistent between device makers. This is a matter where you have to program defensively.
我第一次看到这个的时候,我的老板给我带来了他的手机,一个早期的 Nexus,当时我正在三星 Galaxy II 上开发,它出现在我们的经济新闻提要中他手机上有很多非 ASCII 字符. 因此,这不仅是 Android 内部长期存在的问题,而且在设备制造商之间也不一致。这是您必须进行防御性编程的问题。