1

I want to display the response, coming from the Google Map Server. The response that I am getting is the name of a location that I am getting after sending the latitude and longitude to the Google Map server and the response is coming like this "av de la République" but when I am displaying this in the App after parsing the response, its getting displayed as "av de la RÃ(c)publique"....

The code snippet below might help in understanding my problem..

public static String getAddress(double lat, double lon){
        StringBuilder stringBuilder = new StringBuilder();
         String add ="";

       try {

        HttpPost httppost = new HttpPost("http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lon+"&sensor=false");
        HttpClient client = new DefaultHttpClient();
        org.apache.http.HttpResponse response;
        stringBuilder = new StringBuilder();

            response = client.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

       try {
          add = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
               .getString("formatted_address");
       } catch (Exception e) {
           e.printStackTrace();
       }
       return add;
    }

This is my implementation of the code

public static String getAddress(double lat, double lon){
        StringBuilder stringBuilder = new StringBuilder();
         String add ="";
        try {

        HttpPost httppost = new HttpPost("http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lon+"&sensor=false");
        HttpClient client = new DefaultHttpClient();
        org.apache.http.HttpResponse response;
        stringBuilder = new StringBuilder();


            response = client.execute(httppost);
            HttpEntity entity = response.getEntity();

            char[] buffer = new char[2048];
            Reader reader = new InputStreamReader(entity.getContent(), "UTF-8");
            while (true) {
                int n = reader.read(buffer);
                if (n < 0) {
                    break;
                }
                stringBuilder.append(buffer, 0, n);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            Log.i("===================", stringBuilder.toString());
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


       try {

          add = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
               .getString("formatted_address");

       } catch (Exception e) {
           e.printStackTrace();

       }

       return add;

    }
Gulrez
  • 37
  • 1
  • 3

2 Answers2

2

You are reading the text as bytes, rather than text. Which amounts to reading the response in ISO-8859-1, when in fact it is UTF-8.

Try this:

char[] buffer = new char[2048];
Reader reader = new InputStreamReader(entity.getContent(), "UTF-8");
StringBuilder stringBuilder = new StringBuilder();

while (true) {
    int n = reader.read(buffer);
    if (n < 0) {
        break;
    }
    stringBuilder.append(buffer, 0, n);
}

Here it is integrated into your code:

public static String getAddress(double lat, double lon){
    StringBuilder stringBuilder = new StringBuilder();
    String add ="";

    try {

        HttpPost httppost = new HttpPost("http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lon+"&sensor=false");
        HttpClient client = new DefaultHttpClient();
        org.apache.http.HttpResponse response;
        stringBuilder = new StringBuilder();
        response = client.execute(httppost);
        HttpEntity entity = response.getEntity();

        char[] buffer = new char[2048];
        Reader reader = new InputStreamReader(entity.getContent(), "UTF-8");
        while (true) {
            int n = reader.read(buffer);
            if (n < 0) {
                break;
            }
            stringBuilder.append(buffer, 0, n);
        }

    } catch (ClientProtocolException e) {

    } catch (IOException e) {

    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        add = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
        .getString("formatted_address");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return add;
}
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • Hello Esailija thanks for Answering my question. But I am not getting the desired result till now. Can you please implement your code snippet into mine and explain it a bit. – Gulrez Dec 19 '12 at 11:22
  • @Gulrez I am using `UTF-8` `Reader`, and reading the input to a `StringBuilder`. I have integrated it into your code – Esailija Dec 19 '12 at 13:55
  • Hello Esailija thanks once again for replying to my post after integrating your code into mine, but unfortunately the problem is still not solved... I have posted my implementation of the code, it may help you in finding the problem. – Gulrez Dec 26 '12 at 12:34
-1

This is the answer. https://stackoverflow.com/a/5730210

byte ptext[] = myString.getBytes("ISO-8859-1"); String value = new String(ptext, "UTF-8");

If you are reading from a file you would also need to encode the inputStreamReader with UTF-8 encoding.

final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input, "UTF-8"));

Community
  • 1
  • 1
MAYUR
  • 1
  • 1