我已经拥有以下课程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);
}
}
我对此代码有几个问题:
org.json.JSONException: No value for reviews
错误,我不确定检查的最佳方法是什么。photos[]
数组中获取照片? 答案 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());