java 如何在android中设置字符串字符编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3098207/
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
how to set string character encoding in android
提问by Levara
HI! I have a web page content in encoded in ISO-8859-2. How to convert a stream encoded in this charset to java's UTF-8. I'm trying the code below, but it does not work. It messes up some characters. Is there some other way to do this?
你好!我有一个以 ISO-8859-2 编码的网页内容。如何将此字符集中编码的流转换为 java 的 UTF-8。我正在尝试下面的代码,但它不起作用。它弄乱了一些字符。有没有其他方法可以做到这一点?
BufferedInputStream inp = new BufferedInputStream(in);
byte[] buffer = new byte[8192];
int len1 = 0;
try{
while ( (len1 = inp.read(buffer)) != -1 )
{
String buff = new String(buffer,0,len1,"ISO-8859-2");
stranica.append(buff);
}
回答by king_nak
Try it with an InputStreamReader and Charset:
用 InputStreamReader 和 Charset 试试:
InputStreamReader inp = new InputStreamReader(in, Charset.forName("ISO-8859-2"));
BufferedReader rd = new BufferedReader(inp);
String l;
while ((l = rd.readLine()) != null) {
...
}
If you get an UnsupportedCharsetException, you know what's your problem... Also, with inp.getEncoding()you can check which encoding is really used.
如果你得到一个UnsupportedCharsetException,你就知道你的问题是什么......此外,inp.getEncoding()你可以检查真正使用的是哪种编码。
回答by Michael Borgwardt
How to convert a stream encoded in this charset to java's UTF-8
如何将此字符集中编码的流转换为 java 的 UTF-8
Wrong assumption: Java uses UTF-16 internally, not UTF-8.
错误假设:Java 在内部使用 UTF-16,而不是 UTF-8。
But your code actually looks correct and should work. Are you absolutely sure the webpage is in fact encoded in ISO-8859-2? Maybe its encoding is declared incorrectly.
但是您的代码实际上看起来正确并且应该可以工作。您绝对确定该网页实际上是以 ISO-8859-2 编码的吗?也许它的编码声明不正确。
Or perhaps the real problem is not with the reading code that you've shown, but with whatever code you use to work with the result. How and where do these "messed up characters" manifest?
或者,真正的问题可能不在于您显示的阅读代码,而在于您用来处理结果的任何代码。这些“乱七八糟的角色”是如何以及在哪里表现出来的?

