简体   繁体   中英

google places API returns empty response

I want to use google place API with autocomplete view. I have android and browser key from developer console for places. But when I tried these keys API it returns empty response.

Logcat:

D/Volley: [2651] aa: HTTP response for request=<[ ] 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/Volley: [2650] aa: HTTP response for request=<[ ] 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

my code:

 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();

            }

        }

    }

and in onpostexecute log:

D/YourApp: onPostExecute : 0

i am new in google places api , please help me to find what is wrong.

I think you are not using the correct API key. Follow this guide to properly enable Places API for Android. And this sample by Google to properly implement the Place Auto complete.

Use Web Service key other than using Android Key. And enable it from developer console as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM