Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

My app perform a get request on google maps api to retrieve an address from a 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. It is linked with UTF-8, right?

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 !

like image 508
Poutrathor Avatar asked Dec 26 '22 20:12

Poutrathor


1 Answers

Try using UTF-8,

instead of using InputStream try something like,

String responseBody = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
like image 60
Lalit Poptani Avatar answered Dec 28 '22 09:12

Lalit Poptani