Android Places API会解析所有JSON类别

时间:2014-02-17 05:15:10

标签: android json google-places-api

我已经拥有以下课程GetPlaces,这将获取在我的主要活动onCreate()中调用的Google Places API网址new GetPlaces().execute("https://maps.googleapis.com/maps/api/place/search/json?types=cafe&rankby=distance&location=33.897835,-117.955759&sensor=false&key=MyKey");并解析输出。

private class GetPlaces extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... urls) {

        StringBuilder placesBuilder = new StringBuilder();

        //process search parameter string(s)
        for (String placeSearchURL : urls) {
            //execute search
            HttpClient placesClient = new DefaultHttpClient();

            try {
                //try to fetch the data
                HttpGet placesGet = new HttpGet(placeSearchURL);
                HttpResponse placesResponse = placesClient.execute(placesGet);
                StatusLine placeSearchStatus = placesResponse.getStatusLine();

                if (placeSearchStatus.getStatusCode() == 200) {
                    //we have an OK response
                    HttpEntity placesEntity = placesResponse.getEntity();
                    InputStream placesContent = placesEntity.getContent();
                    InputStreamReader placesInput = new InputStreamReader(placesContent);
                    BufferedReader placesReader = new BufferedReader(placesInput);
                    String lineIn;
                    while ((lineIn = placesReader.readLine()) != null) {
                        placesBuilder.append(lineIn);
                    }
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }


        return placesBuilder.toString();
    }

    protected void onPostExecute(String data) {
        // TODO: check this.exception 
        // TODO: do something with the feed
        /*
        if(placeMarkers!=null){
            for(int pm=0; pm<placeMarkers.length; pm++){
                if(placeMarkers[pm]!=null)
                    placeMarkers[pm].remove();
            }
        }
        */
        super.onPostExecute(data);
        // Dismiss the progress dialog

        if(data !=null)
        {
            JSONObject jsonObj;
            try {
                jsonObj = new JSONObject(data);
                JSONArray results = jsonObj.getJSONArray("results");

                for (int i = 0; i < results.length(); i++) {
                      JSONObject result = results.getJSONObject(i);

                      String name = result.getString("name");
                      /*
                      String icon = result.getString("icon");

                      String address = result.getString("vicinity");
                      String rating = result.getString("rating");
                      String price = result.getString("price_level");
                      String webiste = result.getString("website");
                      //String review = result.getString("reviews");
                      */
                      Log.d("tag", "name: " + name);
                      if (result.getJSONArray("reviews") != null){
                          JSONArray reviewsArray = result.getJSONArray("reviews");
                          JSONObject reviews = reviewsArray.getJSONObject(0);
                          if (reviews != null){
                              String review = reviews.getString("text");  
                              Log.d("tag", "review: " + review);
                          }
                      }




                    }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        }
        //System.out.println(dat);
    }
} 

我对此代码有几个问题:

  1. 我收到org.json.JSONException: No value for reviews错误,我不确定检查的最佳方法是什么。
  2. 怎么能得到这个地方的图像?我不是指图标,但如何在photos[]数组中获取照片?
  3. 如何在Main Activity的onCreate方法或辅助函数中检索此数据?

2 个答案:

答案 0 :(得分:1)

试试这个: - 此网址将帮助您查找/搜索 NearByPlaces .....

  StringBuilder sb = new StringBuilder(
                                            "https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
                                    sb.append("location=" + mLatitude + ","
                                            + mLongitude);
                                    sb.append("&radius=5000");
                                    sb.append("&types=" + type);
                                    sb.append("&sensor=true");
                                    sb.append("&key=--Your--Key--");
        PlacesTask placesTask = new PlacesTask();
    placesTask.execute(sb.toString());

答案 1 :(得分:0)

尝试传递url,如下所示。您还必须将半径也放在url中以查找位置(在您的示例咖啡馆中)。获得地点后,您可以使用标记选项标记地点。因为您可以将图像用作标记

 StringBuilder sb = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
                            sb.append("location="+mLatitude+","+mLongitude);
                            sb.append("&radius=2000");
                            sb.append("&types="+type);
                            sb.append("&sensor=true");

    sb.append("&key=Your+Key");

GetPlaces placesTask = new GetPlaces(); 
placesTask.execute(sb.toString());