【问题标题】:google places API returns empty response谷歌地方 API 返回空响应
【发布时间】:2015-12-17 11:30:11
【问题描述】:

我想使用带有自动完成视图的 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> [寿命=7872],[大小=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 NORMAL 2 1 D/Volley:[2650] a.a:HTTP 响应 请求=https://clients4.google.com/glm/mmap/api0x99e6744e 正常 1> [lifetime=25942], [size=80], [rc=200], [retryCount=1] D/截击:[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 的新手,请帮我找出问题所在。

【问题讨论】:

  • 您是否在 Google 开发者控制台 中启用了google places api 并获得了 API 密钥???
  • @SatyenUdeshi,是的,我启用了安卓键和浏览器键
  • 你能在浏览器中触发 url 并检查它是否返回任何内容吗?
  • 我之前为我的项目启用了地图键,现在我启用了安卓键和浏览器键
  • 您是否使用来自 api 控制台的服务器密钥? url 仅适用于服务器密钥!还要使用浏览器检查您的 URL。如果它不起作用,那么您使用的密钥是错误的。

标签: android google-places-api android-maps-v2 android-maps


【解决方案1】:

我认为您没有使用正确的 API 密钥。按照this guide 正确启用适用于Android 的Places API。而这个 sample 由 Google 正确实现 Place Auto 完成。

【讨论】:

    【解决方案2】:

    使用 Web 服务密钥而不是使用 Android 密钥。 也可以从开发者控制台启用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      • 2018-11-15
      相关资源
      最近更新 更多