[英]Decoding HttpEntity into android string - encoding issue
I know that should be basics but i had no formation :( and I don't understand it, everywhere it seems obvious to people. I get that one side encode data with his set and android is probably expecting another one, but what can I do to translate? 我知道应该是基础知识,但我没有形成 :(我不明白它,在任何地方对人们来说都是显而易见的。我得到那一方用他的设备编码数据和android可能期待另一个,但我能做什么做翻译?
My app perform a get request on google maps api to retrieve an address from a Lat/lng. 我的应用在google maps api上执行get请求,以便从Lat / lng中检索地址。 But I failed to decode properly the result as a French è is displayed as è
但我没有正确解码结果,因为法国è显示为è
I have not enough xp in Java to understand what to do. 我在Java中没有足够的xp来理解该怎么做。 It is linked with UTF-8, right?
它与UTF-8相关联,对吧?
What should I do? 我该怎么办?
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
JSONObject jsonObject = new JSONObject();
jsonObject = new JSONObject(stringBuilder.toString());
retList = new ArrayList<Address>();
if("OK".equalsIgnoreCase(jsonObject.getString("status"))){
JSONArray results = jsonObject.getJSONArray("results");
for (int i=0;i<results.length();i++ ) {
JSONObject result = results.getJSONObject(i);
String indiStr = result.getString("formatted_address");
Address addr = new Address(Locale.ITALY);
addr.setAddressLine(0, indiStr);
Dbg.d(TAG, "adresse :"+addr.toString());
retList.add(addr);
}
}
Thanks for help ! 感谢帮助 !
Try using UTF-8, 尝试使用UTF-8,
instead of using InputStream
try something like, 而不是使用
InputStream
尝试类似的东西,
String responseBody = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
you can use BufferReader 你可以使用BufferReader
your code will be like this: 你的代码将是这样的:
InputStream stream = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
int b;
while ((b = br.read()) != -1) {
stringBuilder.append(b);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.