Android JSON CharSet UTF-8问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android JSON CharSet UTF-8问题相关的知识,希望对你有一定的参考价值。
我目前正在开发一个android应用程序,它从提供JSON数据的API获取数据。我将JSON数据的项目存储为字符串,这导致出现一些奇怪的字符(例如'Â')。我理解这与字符集有关,因此我将InputStreamReader设置为“UTF-8”,但它似乎没有解决问题。
URL hukd = new URL(requestUrl);
URLConnection tc = hukd.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream(), "UTF-8"));
String line = in.readLine();
Log.d("line :", line);
JSONObject obj = new JSONObject(line);
JSONArray ja = obj.getJSONObject("deals").getJSONArray("items");
for (int i = 0; i < ja.length(); i++) { // each deal
JSONObject jo = (JSONObject) ja.get(i);
// make the deal
Deal d = new Deal(jo.getString("title"),
jo.getString("description"),
jo.getString("price"),jo.getString("temperature"),
jo.getJSONObject("merchant").getString("name"),
jo.getString("deal_image"),
jo.getString("deal_image_highres"));
listItems.add(d);
Log.d("Deal:", d.toString());
}
Log.d“line”给出
01-30 19:56:01.909: D/line :(610): {"deals":{"items":[{"title":"Absolute steal ** Harmony one remote in store only Comet u00c2u00a349.98 **","deal_link":"http://www.hotukdeals.com/deals/absolute-steal-harmony-one-remote-store-only-comet-49-98-1131779?aui=465","mobile_deal_link":"http://m.hotukdeals.com/deals/absolute-steal-harmony-one-remote-store-only-comet-49-98-1131779?aui=465","deal_image":"http://www.hotukdeals.com/images/threads/1131779_1.jpg","description":"Was just in comet getting two new washing machines when I stumbled upon a load of Harmony ones on the end of an isle u00e2u0080u00a6. I nearly blew a fuse when I saw they had been reduced from u00c2u00a380.00 to under u00c2u00a350 as I just bought 3 of these before xmas at u00c2u00a368.00 ea which I thought was a true bargain.
I asked they guy and he said they were stopping doing them as far as he knew and that the deal should be nation wide this must be the cheapest I've seen for the UK version ever even though this is the Harmony one and not the one + is it still a great remote which does just about everything you could want it to.
** note I donu00e2u0080u0099t have a picture of the price tag but I have these at my home and are 100% that itu00e2u0080u0099s the harmony one on sale hope this helps some of you guys and girls out **
The Logitech Harmony One remote control lets you replace 15 remotes with one, easy to use device. With its clear, touch screen display this Harmony learning remote control makes controlling the most complex of systems easy. A graphic interface allows you, for example, to watch a DVD at the touch of a button.
","submit_time":"48 minutes ago","hot_time":"1 minute ago","poster_name":"no1son","temperature":106.88999939,"price":49.98,"timestamp":1327950390,"expired":"false","forum":{"name":"Deals","url_name":"deals"},"category":{"name":"Audiovisual","url_name":"audiovisual"},"merchant":{"name":"Comet","url_name":"comet.co.uk"},"tags":{"items":[{"name":"leeds"}]},"deal_image_highres":"http://www.hotukdeals.com/images/threads/high-res/1131779_1.jpg"},{"title":"Youth Brazil Shirt u00c2u00a34 at Very/Littlewoods","deal_link":"http://www.hotukdeals.com/deals/youth-brazil-shirt-4-very-littlewoods-1131765?aui=465","mobile_deal_link":"http://m.hotukdeals.com/deals/youth-brazil-shirt-4-very-littlewoods-1131765?aui=465","deal_image":"http://www.hotukdeals.com/images/threads/1131765_1.jpg","description":"2010 Brazil shirt by Nike
Available in all youth sizes.
Free delivery through collect plus","submit_time":"1 hour, 4 minutes ago","hot_time":"14 minutes ago","poster_name":"ericagradus","temperature":161.479995728,"price":4,"timestamp":1327949447,"expired":"false","forum":{"name":"Deals","url_name":"deals"},"category":{"name":"Fashion","url_name":"fashion"},"merchant":{"name":"Very","url_name":"very.co.uk"},"tags":{"items":[{"name":"brazil shirt"},{"name":"very"},{"name":"littlewoods"}]},"deal_image_highres":"http://www.hotukdeals.com/images/threads/high-res/1131765_1.jpg"},{"title":"Milk Chocolate Mikado 29p at Home Bargains","deal_link":"http://www.hotukdeals.com/deals/milk-chocolate-mikado-29p-home-bargains-1130742?aui=465","mobile_deal_link":"http://m.hotukdeals.com/deals/milk-chocolate-mikado-29p-home-bargains-1130742?aui=465","deal_image":"http://www.hotukdeals.com/images/threads/1130742_1.jpg","description":"Was in Home Bargains in Lancaster and they had the MIlk Chocolate Mikado for 29p. The white and hazelnut were 59p. Didn't check the sell by day as it took all my will power to resist haha!
Sorry if this is a duplicate I did look for it but couldn't find it.","submit_time":"1 day, 3 hours ago","hot_time":"14 minutes ago","poster_name":"cazi77","temperature":146.61000061,"price":0.29,"timestamp":1327853963,"expired":"false","forum":{"name":"Deals","url_name":"deals"},"category":{"name":"Groceries","url_name":"groceries"},"merchant":{"name":"Home Bargains","url_name":"homebargains.co.uk"},"tags":{"items":[]},"deal_image_highres":"http:/
有任何想法吗?
尝试以下代码段代码,当我遇到类似问题时帮助了我:
new String(jo.getString("name").getBytes("ISO-8859-1"), "UTF-8");
我知道这是几年之后,但我提出了比这个更可靠的解决方案,并且它不需要所有编码额外的东西
这是偶然发现我寻找其他东西,但它的工作原理:
"" + jo.opt("name");
这样做是将结果作为原始对象,并将其放在空字符串的末尾强制它成为字符串格式 - 当然,如果条目原来不是字符串,它可以给出模糊的结果,所以也许尝试抓取它首先是一个字符串
我有这样的事情:
String s = "";
try {
s = jo.getString("name");
s = "" + jo.opt("name");
} catch (Exception e) {
Log.e("JSON error", "error:" + e.toString());
}
这将测试字符串是否存在,然后将其替换为完全编码的版本
我希望将来可以帮助某人:-)
以上是关于Android JSON CharSet UTF-8问题的主要内容,如果未能解决你的问题,请参考以下文章
NSMutableURLRequest setValue:forHTTPHeaderField xcode 6.2 不适用于“application/json; charset=utf-8”
HTTP 415 无法处理消息,因为内容类型为 'application/json; charset=utf-8' 不是预期的类型 'text/xml;字符集=utf-8'
Spring boot 控制器调用不支持内容类型“application/json;charset=UTF-8”
未找到 Java 类型和 MIME 媒体类型 application/json;charset=utf-8 的消息正文阅读器
application/json;charset=utf-8 控制器怎么接口
记录ajax 设置请求header的Content-Type 为 application/json;charset=utf8