我如何将字符串转换为UTF-8的Andr​​oid? [英] How do I convert a string to UTF-8 in Android?

查看:167
本文介绍了我如何将字符串转换为UTF-8的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是HTML解析器叫Jsoup,加载和解析HTML文件。问题是,我刮的网页是EN $ C $光盘 ISO-8859-1 字符集而Android是使用 UTF-8 编码(?)。这是导致某些字符显示为问号。

所以,现在我想我应该将字符串转换为UTF-8格式。

现在我发现这个所谓的类 CharsetEn $ C在Android SDK,我想能不能帮我$ CR 。但我无法弄清楚如何实现它在实践中,所以我不知道是否能得到帮助,SOM通过一个实际的例子。

更新:code读取数据( Jsoup

  URL =新的URL(http://www.example.com);
文档的DOC = Jsoup.parse(URL,4000);


解决方案

您可以让Android的做的工作为你通过阅读网页变成一个byte [],然后用解析String对象的jSoup方法。

不要忘了指定编码当您使用正确的字符串<一个从服务器读取数据字符串href=\"http://developer.android.com/reference/java/lang/String.html#String%28byte%5b%5d,%20java.lang.String%29\">constructor.

I am using a HTML parser called Jsoup, to load and parse HTML files. The problem is that the webpage I'm scraping is encoded in ISO-8859-1 charset while Android is using UTF-8 encoding(?). This is results in some characters showing up as question marks.

So now I guess I should convert the string to UTF-8 format.

Now I have found this Class called CharsetEncoder in the Android SDK, which I guess could help me. But I can't figure out how to implement it in practice, so I wonder if could get som help with by a practical example.

UPDATE: Code to read data (Jsoup)

url = new URL("http://www.example.com");
Document doc = Jsoup.parse(url, 4000);

解决方案

You can let Android do the work for you by reading the page into a byte[] and then using the jSoup methods for parsing String objects.

Don't forget to specify the encoding when you create the string from the data read from the server using the correct String constructor.

这篇关于我如何将字符串转换为UTF-8的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆