我想传递地址并想要 lat 和 long 。但我从 maps.google.com 获得了完整的字符串。但是JSON格式有错误。
我尝试下面的代码
Error :Couldn't get connection factory client
getLocationInfo("3 cité Nollez Paris France");
public static void getLocationInfo(String address) {
StringBuilder stringBuilder = new StringBuilder();
try {
address = address.replaceAll(" ","%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
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);
}
System.out.print(stringBuilder);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
System.out.print(longitute);
latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
System.out.print(latitude);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return jsonObject;
}
LOGCAT
{
"results" : [
{
"address_components" : [
{
"long_name" : "3",
"short_name" : "3",
"types" : [ "street_number" ]
},
{
"long_name" : "Cité Nollez",
"short_name" : "Cité Nollez",
"types" : [ "route" ]
},
{
"long_name" : "18th arrondissement of Paris",
"short_name" : "18th arrondissement of Paris",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Ã?le-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
},
{
"long_name" : "75018",
"short_name" : "75018",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "3 Cité Nollez, 75018 Paris, France",
"geometry" : {
"location" : {
"lat" : 48.89376110,
"lng" : 2.33742180
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 48.89511008029150,
"lng" : 2.338770780291502
},
"southwest" : {
"lat" : 48.89241211970850,
"lng" : 2.336072819708498
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
03-14 13:51:35.776: I/MapActivity(1430): Handling network change notification:CONNECTED
03-14 13:51:35.776: E/MapActivity(1430): Couldn't get connection factory client