我想在自动填充视图中使用google place API。我有来自开发者控制台的android和浏览器密钥。但是当我尝试这些键时 它返回空响应的API。
logcat的:
D / Volley:[2651] a.a:请求的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正常2> [lifetime = 7872],[size = 0],[rc = 204], [retryCount = 0] D / Volley:[1] p.b: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正常2 1 D / Volley:[2650] a.a:HTTP响应 request =< [] https://clients4.google.com/glm/mmap/api 0x99e6744e 标准1> [lifetime = 25942],[size = 80],[rc = 200],[retryCount = 1] D / Volley:[1] p.b:25943 ms:[] https://clients4.google.com/glm/mmap/api 0x99e6744e正常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的新用户,请帮我找出错误。
答案 0 :(得分:0)
我认为您没有使用正确的API密钥。按照this guide正确启用Android版Places API。而且sample由Google正确实施Place Auto完成。
答案 1 :(得分:0)
除使用Android Key外,请使用Web服务密钥。 并从开发人员控制台启用它。