我正在尝试从以下 json 中获取位置名称
http://maps.googleapis.com/maps/api/geocode/json?latlng=18.486096,73.802027&sensor=false返回 JSON 的特定地址。我正在开发一个 android 应用程序来使用 api 反向地理编码。
这是下面的代码..
public class MyGeocoder
{
public static String getUserLocation(Location loc) {
String userlocation = null;
String readUserFeed = readUserLocationFeed((Double.toString(loc.getLatitude())) +
","+( Double.toString(loc.getLongitude())));
try {
//JSONObject Strjson = new JSONObject(readUserFeed);
JSONArray jsonArray = new JSONArray(readUserFeed);
JSONObject jsonObject=jsonArray.getJSONObject(0);
userlocation = jsonObject.getString("locality").toString();
} catch (Exception e) {
e.printStackTrace();
}
Log.i("User Location ", userlocation);
return userlocation;
}
public static String readUserLocationFeed(String address) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?
latlng="+ address + "&sensor=false");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
//Log.e(ReverseGeocode.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
}