1
votes

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] a.a: 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] 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 response for request=<[ ] https://clients4.google.com/glm/mmap/api 0x99e6744e NORMAL 1> [lifetime=25942], [size=80], [rc=200], [retryCount=1] D/Volley: [1] p.b: 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.

2
Have you enabled google places api in the Google developer console and got an API key ??? - Satyen Udeshi
@SatyenUdeshi, yup i enabled android key and brower key - samira
Can you fire the url in browser and check if it returns anything ? - Satyen Udeshi
i had enabled map key for my project before and now i enabled android key and brower key - samira
are you using server key from api console? url works with server key only! Also check your URL wiht browser. If its not working, then the key you used is wrong. - ravidl

2 Answers

0
votes

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.

0
votes

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