2

我想传递地址并想要 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
4

3 回答 3

2

在您的 Map 活动中添加以下代码在 super.onCreate() 之前

示例活动:-

 public class ExampleActivity extends Activity {

    // url to make request
    private static String url = "http://maps.googleapis.com/maps/api/geocode/json?address="+address.replace(" ", "%20")+"&sensor=true";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tvlat = (TextView) findViewById(R.id.tvlat);
        tvlng = (TextView) findViewById(R.id.tvlng);

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
             System.out.println("Location Latitude"
                + json.getJSONArray("results").getJSONObject(0)
                        .getJSONObject("geometry")
                        .getJSONObject("location").getString("lat"));

               System.out.println("Location Longitude"
                + json.getJSONArray("results").getJSONObject(0)
                        .getJSONObject("geometry")
                        .getJSONObject("location").getString("lng"));    
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

JSONPareser:-

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();            

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

然后运行你的应用程序,它对我来说很好。

于 2013-03-14T08:49:54.193 回答
0

您是否启用了这些权限。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
于 2013-03-14T08:25:46.097 回答
0

如果在使用地图时“无法获得连接错误”,则无需担心。即使发生这种情况,它也会正常工作。如果您的代码不起作用,那么还有其他原因。检查它..

于 2013-03-14T09:33:30.350 回答