0

I'm using this code to get address info and it is working fine, but the casting for non english characters is not correct.

How to fix unicode problem??

 public JSONObject getLocationInfo() {
    //Http Request
    HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {//even Character.toChars(b) not working
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
        } catch (IOException e) {
    }
            //Create a JSON from the String that was return.
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return jsonObject;
}

call the function as follows to get the complete address:

JSONObject ret = getLocationInfo(); //Get the JSON that is returned from the API call
JSONObject location;
 String location_string;
  //Parse to get the value corresponding to `formatted_address` key. 
  try {
  location = ret.getJSONArray("results").getJSONObject(0);
  location_string = location.getString("formatted_address");
  Log.d("test", "formattted address:" + location_string);
  } catch (JSONException e1) {
  e1.printStackTrace();

 }
Community
  • 1
  • 1

1 Answers1

1

Instead of reading byte wise into a StringBuilder, use

ByteArrayOutputStream baos;
...

String jsonText = new String(baos.toByteArray(), StandardCharsets.UTF_8);

In concreto:

ByteArrayOutputStream baos = new ByteArrayOutputStream();

try {
    response = client.execute(httpGet);
    HttpEntity entity = response.getEntity();
    InputStream stream = entity.getContent();
    byte[] buffer = new byte[1024];
    int nread;
    while ((nread = stream.read(buffer)) > 0) {
        baos.write(buffer, 0, nread);             
    }
} catch (ClientProtocolException | IOException e) {
}
//Create a JSON from the String that was return.
JSONObject jsonObject = new JSONObject();
try {
    String jsonText = new String(baos.toByteArray(), StandardCharsets.UTF_8);
    jsonObject = new JSONObject(jsonText);

The above uses a buffer too; there exists a BufferedInputStream too.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Where i should use it? can you write the code please – user3269815 Mar 10 '14 at 12:01
  • This compiler error shown `Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties.`, i think not working for android. – user3269815 Mar 10 '14 at 14:20
  • even this `String jsonText = new String(baos.toByteArray(), StandardCharsets.UTF_8);` not working – user3269815 Mar 10 '14 at 14:20
  • Or replace CharSet value `StandardCharsets.UTF_8` with String `"UTF-8"`. The StandardCharsets was more recently introduced to be both typesafe and guarantee the existence of the encoding, hence no UnsupportedCharacterEncodingException needs to be cached. The same 1.7 construct for catch with multiple exceptions. – Joop Eggen Mar 10 '14 at 14:42