简体   繁体   English

如何处理来自谷歌地图服务器的重音字符(如 République 中的 é)作为 Android 中的响应

[英]How to handle Accent characters (like é in the word République) coming from the Google Map server as response in Android

I want to display the response, coming from the Google Map Server.我想显示来自 Google 地图服务器的响应。 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"....我得到的响应是将纬度和经度发送到谷歌地图服务器后我得到的位置的名称,响应是这样的“av de la République”,但是当我在应用程序中显示它之后解析响应,它显示为“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;

    }

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.这相当于读取 ISO-8859-1 中的响应,而实际上它是 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;
}

This is the answer.这就是答案。 https://stackoverflow.com/a/5730210 https://stackoverflow.com/a/5730210

byte ptext[] = myString.getBytes("ISO-8859-1"); byte ptext[] = myString.getBytes("ISO-8859-1"); String value = new String(ptext, "UTF-8"); 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.如果您正在读取文件,您还需要使用 UTF-8 编码对 inputStreamReader 进行编码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android java的口音即将破碎 - android java accent coming broken 像浏览器一样来自片段的Android Google Map - Android google map from a fragment like a browser 如何处理从移动Chrome浏览器到Android本机(例如短信,Google PlayStore)的切换,反之亦然? - How to handle switching from mobile chrome browser to android native like sms, google playstore and vice-versa? 如何从没有重音的单词中获取所有强调单词? - How to get all the emphasized words from a word without an accent? 响应不是来自使用改造 2 的服务器 - Response is not coming from server using retrofit2 提交表单后如何处理服务器的响应? - How to Handle the response from server after a form submission? 如何处理来自Android端的Java Web Service中的Json对象 - How to handle Json object in java web service coming from android side 处理来自Throwable catch的NullPointerException的最佳方法? (Android) - Best way to handle a NullPointerException coming from a Throwable catch? (Android) 带有PHP的Android JSON,带有重音符号的字符未正确处理 - Android JSON with PHP, characters with accent not being handled properly 从服务器收到的XML响应中的特殊字符 - Special characters in XML response received from server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM