gpt4 book ai didi

java - 如何处理来自 Google map 服务器的重音字符(如 République 中的 é)作为 Android 中的响应

转载 作者:行者123 更新时间:2023-11-30 03:52:25 26 4
gpt4 key购买 nike

我想显示来自 Google map 服务器的响应。我得到的响应是我在将经纬度发送到 Google map 服务器后得到的位置名称,响应是这样的“av de la République”,但是当我在应用程序中显示它之后解析响应,它显示为“av de la RÃ(c)publique”...

下面的代码 fragment 可能有助于理解我的问题..

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;
}

这是我实现的代码

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;

}

最佳答案

您正在以字节而不是文本的形式读取文本。金额是多少读取 ISO-8859-1 中的响应,而实际上它是 UTF-8。

试试这个:

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);
}

此处它已集成到您的代码中:

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;
}

关于java - 如何处理来自 Google map 服务器的重音字符(如 République 中的 é)作为 Android 中的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13935481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com