0

I am using the code from the question asked here Autocomplete textview google places api description -> place_id. I changed the code to add it to my app. When I run the code it is giving me the below response when I run the code

07-28 11:17:19.244: E/result(350): {"error_message":"This API project is not authorized to use this API. Please ensure that this API is activated in the APIs Console: Learn more: https:\/\/code.google.com\/apis\/console","predictions":[],"status":"REQUEST_DENIED"}

I searched for some threads and from the below threads

  1. Maps API keys for geocoding not working
  2. This IP, site or mobile application is not authorized to use this API key and one or two other

I did the follow steps as suggested.

  1. Created a Server Key from console and added it to project.
  2. Enabled Places API from console.
  3. Enabled Geocoding API from console.

You can see in below image. enter image description here

and when I run the code it still shows me the error mentioned above. Do I need to Enable any other APIs from console.

The maps are working good except this. In case you want to know this is my api key added in manifest.

<!-- Goolge Maps API Key Development mode -->
            <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAbo3luzbyQTuuPLOyo_Ml***********" />

and I am adding server key directly to request url as below

private ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
    HttpURLConnection conn = null;
    StringBuilder jsonResults = new StringBuilder();
    try {
        StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
        sb.append("?key=" + API_KEY);
        sb.append("&input=" + URLEncoder.encode(input, "utf8"));

        URL url = new URL(sb.toString());
        conn = (HttpURLConnection) url.openConnection();
        InputStreamReader in = new InputStreamReader(conn.getInputStream());

        // Load the results into a StringBuilder
        int read;
        char[] buff = new char[1024];
        while ((read = in.read(buff)) != -1) {
            jsonResults.append(buff, 0, read);
        }
    } catch (MalformedURLException e) {
        Log.e(LOG_TAG, "Error processing Places API URL", e);
        return resultList;
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error connecting to Places API", e);
        return resultList;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }

    try {
        // Create a JSON object hierarchy from the results
        JSONObject jsonObj = new JSONObject(jsonResults.toString());
        JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
        Log.e("result",jsonObj.toString());
        // Extract the Place descriptions from the results
        resultList = new ArrayList<String>(predsJsonArray.length());
        for (int i = 0; i < predsJsonArray.length(); i++) {
            resultList.add(predsJsonArray.getJSONObject(i).getString("description")+ "," + predsJsonArray.getJSONObject(i).getString("place_id") );
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Cannot process JSON results", e);
    }
    return resultList;
}

Please Help.

7
  • did you created google places api key? Commented Jul 28, 2015 at 6:03
  • i created server key as mentioned, maps are working fine except this.
    – karan
    Commented Jul 28, 2015 at 6:04
  • please send your manifest.xml Commented Jul 28, 2015 at 6:04
  • @KaranMer not server key... you have to create google places api key... Commented Jul 28, 2015 at 6:06
  • there is no such thing like google places api key. you can see from docs that you need only server key developers.google.com/places/webservice/intro
    – karan
    Commented Jul 28, 2015 at 6:08

1 Answer 1

6

Needed some more thorough research and after some trial and error it worked. main thing was that I needed to enable Google Places API Web Service. and it worked like charm.

Hope this helps someone out there.

5
  • This is the correct answer. Since you are manually constructing HTTP requests, and sending them to the Web Service endpoint, you need to have the Web Service API enabled. Google also has a native SDK for Android which provides Autocomplete, which is worth looking into: developers.google.com/places/android
    – plexer
    Commented Jul 29, 2015 at 1:35
  • Great.you saved my day buddy. :)
    – Rohan
    Commented Oct 3, 2015 at 8:10
  • Nice answer. Working perfect for me also. Only, need to generate server or Browser key for this not android key in that google project after enabling Google Places API Web Service.. Commented Oct 29, 2015 at 11:05
  • Working perfectly. Just let me know the geocpding api is also needed? Commented Dec 16, 2015 at 6:10
  • Answer is correct but seems major glitch in google places api for mobile
    – VVB
    Commented Mar 29, 2016 at 5:52