[英]google places API returns empty response
我想将Google Place API与自动填充视图一起使用。 我有来自开发人员控制台的Android和浏览器密钥。 但是,当我尝试这些密钥API时,它将返回空响应。
Logcat:
D / Volley:[2651] aa:请求的HTTP响应= <[] https://csi.gstatic.com/csi?s=maps_android_api&v=3&action=map_start_up&it=map_load.10792,on_create.0,on_resume.2,init .23&irt = 10792,23,76,23 0x4d844933 NORMAL 2> [lifetime = 7872],[size = 0],[rc = 204],[retryCount = 0] D / Volley:[1] pb:7903 ms:[ ] https://csi.gstatic.com/csi?s=maps_android_api&v=3&action=map_start_up&it=map_load.10792,on_create.0,on_resume.2,init.23&irt=10792,23,76,23 0x4d844933 NORMAL 2 1 D /凌空:[2650] aa:请求的HTTP响应= <[] https://clients4.google.com/glm/mmap/api 0x99e6744e NORMAL 1> [lifetime = 25942],[size = 80],[rc = 200 ],[retryCount = 1] D / Volley:[1] pb:25943 ms:[] https://clients4.google.com/glm/mmap/api 0x99e6744e NORMAL 1
我的代码:
class GetPlaces extends AsyncTask<String, Void, ArrayList<String>> {
private ArrayAdapter<String> adapter;
@Override
protected ArrayList<String> doInBackground(String... args) {
ArrayList<String> predictionsArr = new ArrayList<String>();
try {
URL googlePlaces = new URL(
"https://maps.googleapis.com/maps/api/place/autocomplete/json?input="
+ URLEncoder.encode(args[0].toString(),
"UTF-8")
+ "&types=geocode&language=en&sensor=true&key=mykey");
URLConnection tc = googlePlaces.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(tc.getInputStream()));
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line);
}
JSONObject predictions = new JSONObject(sb.toString());
JSONArray ja = new JSONArray(predictions
.getString("predictions"));
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
// add each entry to our array
predictionsArr.add(jo.getString("description"));
}
} catch (IOException e) {
} catch (JSONException e) {
}
return predictionsArr;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
// put all the information in the list view to display
Log.d("YourApp", "onPostExecute : " + result.size());
// update the adapter
adapter = new ArrayAdapter<String>(mActivity,
R.layout.simple_list_item);// show the list view as texts
// attach the adapter to listView
listViewSearch.setAdapter(adapter);
for (String string : result) {
adapter.add(string);
adapter.notifyDataSetChanged();
}
}
}
并在onpostexecute日志中:
D/YourApp: onPostExecute : 0
我是Google Places API的新手,请帮助我找出问题所在。
使用Web服务密钥而不是使用Android密钥。 并从开发人员控制台启用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.