0

我正在做一个应用程序,它请求某种类型的最近地点并在列表中返回每个结果的名称、坐标和信息,我正在为此使用 google places api。

我的代码如下

Helper.java具有将 url 发送到 google 服务器并检索 xml 文件并将其解析为包含名称、坐标和信息的对象列表的方法:

public Document getDocument(int radius, String type, LatLng current) {

        String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?"
                + "location="
                + current.latitude
                + ","
                + current.longitude
                + "&radius="
                + radius
                + "&types="
                + type
                + "&sensor=true&key=MyKeyHere"; //i replace this with my api key

        Log.d("URL", url);

        try {

            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse response = httpClient.execute(httpPost, localContext);
            InputStream in = response.getEntity().getContent();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            Document doc = builder.parse(in);
            Log.d("Document content", doc.getTextContent()); 
            return doc;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public ArrayList<MyLocation> getNearbyLocations(Document doc) {
        Log.d("DOC**", doc.getTextContent());

        ArrayList<MyLocation> nearbyLocations = new ArrayList<MyLocation>();
        String name;
        double lati;
        double longi;
        String info;
        // MyLocation theLocation;

        NodeList nLName = doc.getElementsByTagName("name");
        NodeList nLLat = doc.getElementsByTagName("lat");
        NodeList nLLong = doc.getElementsByTagName("lng");
        NodeList nLvicinity = doc.getElementsByTagName("vicinity");
        for (int i = 0; i < nLName.getLength(); i++) {

            name =  nLName.item(i).getNodeValue();
            lati =  Double.parseDouble(nLLat.item(i).getNodeValue());
            longi = Double.parseDouble(nLLong.item(i).getNodeValue());
            info =  nLvicinity.item(i).getNodeValue();
            nearbyLocations.add(new MyLocation(name, new LatLng(lati, longi),
                    info));
            Log.d("LIST***", nearbyLocations.toString());
        }

        return nearbyLocations;
    }

搜索.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);




        Helper= new Helper();


        LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, ls);

        Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        ls.onLocationChanged(location);

     docu = Helper.getDocument(5000, "airport" ,new LatLng(alti, longi)); 
        nearByLocation= Helper.getNearbyLocations(docu); ////it gives me a nullpointerexception here

        //Log.d("DOC***", docu.getTextContent());

        listView = (ListView) findViewById(R.id.my_list2);
        adapter = new ItemAdapter(this, getItems());
        listView.setAdapter(adapter);

    }

问题是程序在调用 getNearByLocations(doc) 方法时崩溃,我认为问题是检索到的文档始终为空,所以我不知道我检索 XML 文件的方法是否错误。

这是我的日志:

10-04 19:40:43.049: E/AndroidRuntime(1737): Caused by: java.lang.NullPointerException
10-04 19:40:43.049: E/AndroidRuntime(1737):     at com.Gourp_KSA.tourfit.Helper.getNearbyLocations(Helper.java:95)
10-04 19:40:43.049: E/AndroidRuntime(1737):     at com.Gourp_KSA.tourfit.Search.onCreate(Search.java:85)
10-04 19:40:43.049: E/AndroidRuntime(1737):     at android.app.Activity.performCreate(Activity.java:5104)
10-04 19:40:43.049: E/AndroidRuntime(1737):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-04 19:40:43.049: E/AndroidRuntime(1737):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

编辑和回答:
所以我发现了问题所在。那是我在主线程上建立网络连接。我创建了一个 AsyncTask 类来调用 getNearbyLocations() 方法。我希望这有帮助

4

1 回答 1

0

当我认为您应该向 Google Places API 发送 HTTP GET 请求时,您似乎正在使用 HTTP POST 请求。

如果您有兴趣使用为您完成所有这些工作并允许您通过 Java 方法和对象与 Google Places API 交互的 Java 库,您可以尝试Sprockets(披露:我是开发人员)。

获取对象的等效 Sprockets 调用ListPlace是:

Places.nearbySearch(Params.create().latitude(lat).longitude(lng)
        .radius(5000).types("airport")).getResult();
于 2013-10-04T13:34:34.297 回答