GoogleMapServerからの応答を表示したい。取得した応答は、緯度と経度をGoogleマップサーバーに送信した後に取得した場所の名前であり、応答は「av delaRépublique」のようになりますが、これをアプリに表示すると、応答を解析すると、「av delaRÃ(c)publique」と表示されます。
以下のコードスニペットは、私の問題を理解するのに役立つかもしれません。
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;
}