0

I have used google place api to get the places in auto complete textview.After researching i have made a application.But when i run the application i get the below error

ERROR

This IP, site or mobile application is not authorized to use this API key

CODE

public class GetPlaces extends AsyncTask<String, String, String> {

    private Context context;

    private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
    private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
    private static final String OUT_JSON = "/json";

    private static final String API_KEY = "key=My Server Key";

    private String INPUT = "INPUT";

    private HttpURLConnection urlConnection = null;
    private StringBuilder stringBuilder = null;
    private URL url;
    private InputStream iStream = null;


    public GetPlaces(Context context, String INPUT) {
        this.context = context;
        this.INPUT = INPUT;
        this.execute(INPUT);
    }

    @Override
    protected String doInBackground(String... params) {

        String response = "";

        try {
            INPUT = "input=" + URLEncoder.encode(params[0], "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();

        }

        String types = "types=geocode";

        // Sensor enabled
        String sensor = "sensor=false";

        // Building the parameters to the web service
        String parameters = INPUT + "&" + types + "&" + API_KEY + "&" + sensor;

        stringBuilder = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
        stringBuilder.append("?" + parameters);


        //connections is established here
        try {
            url = new URL(stringBuilder.toString());

            urlConnection = (HttpURLConnection) url.openConnection();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

            StringBuffer sb = new StringBuffer();

            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            response = sb.toString();
            Log.e("Response", response);

            br.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                iStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            urlConnection.disconnect();
        }

        return response;
    }
}

P.S-I know this has been already answered stating that u dont need Android key but you Server Key,so for that i have used my IP Address but still the same error.

Anuj
  • 367
  • 2
  • 6
  • 18

1 Answers1

0

Please check the link and Create a project & generate API key. Use that API key on below line:

 private static final String API_KEY = "key=My Server Key";

For your above code the API key will be the server API key, not Android app API key. If you still find any issue, let me know.

sUndeep
  • 362
  • 3
  • 16
  • Yes sir i have mentioned that i am using my local IP address to generate the server key – Anuj Feb 09 '15 at 08:32
  • Please don't provide any IP address there. Simply blank box and generate. – sUndeep Feb 09 '15 at 08:35
  • OK .Now sir can you please tell me how will i find the lat and long of that selected place?? – Anuj Feb 09 '15 at 08:43
  • So is your problem solved? Is this message still coming: "This IP, site or mobile application is not authorized to use this API key"? – sUndeep Feb 09 '15 at 08:45
  • Okay great. Then please mark this answer as resolved. Then next: Have you succeeded to get the lat & long? – sUndeep Feb 09 '15 at 11:01
  • Nah i dont know how to do that – Anuj Feb 09 '15 at 11:23
  • Please check the link. In case didn't get let me know: http://stackoverflow.com/questions/13189497/android-providing-auto-autosuggestion-in-android-places-api – sUndeep Feb 09 '15 at 11:32