0

I'm trying to get location address from coordinates on my Android app. As mentioned here, I got myself an API key and tested it with the standard URL on browser, which returned successful response. But when I tried the same URL on my Android app, this -

{

(just an opening curly brace) is the response I got.

Here's my Java code:

if (bestLocation != null) {
                        locationLatitude = bestLocation.getLatitude();
                        locationLongitude = bestLocation.getLongitude();
                        String latlng = locationLatitude + "," + locationLongitude;
                        final String location_type = "RANGE_INTERPOLATED";
                        final String API_KEY = "MY_WORKING_KEY";
                        final String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng="
                                + latlng + "&location_type=" + location_type + "&key=" + API_KEY;
                        new GetLocationAddress(CreateUser.this).execute(url);
                    }

GetLocationAddress

    private static class GetLocationAddress extends AsyncTask<String, Void, String> {
        private WeakReference<CreateUser> reference;

        GetLocationAddress(CreateUser context) {
            reference = new WeakReference<>(context);
        }

        @Override
        protected String doInBackground(String... strings) {
            try {
                HttpURLConnection connection = (HttpURLConnection) new URL(strings[0]).openConnection();
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String result = reader.readLine();
                connection.disconnect();
                Log.i("JSON Response is ", result);                
                return result;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

All the parameters in the URL are working on browser. I've tried using the API as both unrestricted and restricted to Android apps by setting SHA-1 fingerprints for both debug and release keystores, but the result is the same. Please help.

Update:

I've checked the API console and found that all of my requests have been treated as forbidden (error 403), even while the API is restricted to Android apps with fingerprints of my app provided. Package names for both debug and release fingerprints are the same. It's also been observed that the API request count gets updated in the console only a little while after my app receives the response.

Nithin
  • 221
  • 3
  • 14

1 Answers1

0

Use AsyncTask like this.

private class GetLocationAddress extends AsyncTask<String, Void, String> {
    String server_response;
    private WeakReference<CreateUser> reference;

    GetLocationAddress(CreateUser context) {
        reference = new WeakReference<>(context);
    }

    @Override
    protected String doInBackground(String... strings) {
        URL url;
        HttpURLConnection urlConnection = null;
        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            int responseCode = urlConnection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                server_response = readStream(urlConnection.getInputStream());
                Log.v("CatalogClient", server_response);
            }

            urlConnection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Log.e("Response", "" + server_response);
    }
}


// Converting InputStream to String
private String readStream(InputStream in) {
    BufferedReader reader = null;
    StringBuffer response = new StringBuffer();
    try {
        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return response.toString();
}

To Call this AsyncTask class

new GetLocationAddress(CreateUser.this).execute(url);
Dharmishtha
  • 1,313
  • 10
  • 21
  • Please see the update. My request is forbidden. That's the problem. – Nithin Dec 12 '17 at 10:53
  • check in your console . if API request is reaches to 2500 then it is forbidden. have look to this . https://stackoverflow.com/questions/18743128/recieving-a-403-forbidden-error-when-using-latitude-and-longitude-geocoding – Dharmishtha Dec 12 '17 at 11:10
  • There's only been around 10 requests to this point. It's not usage limit. – Nithin Dec 12 '17 at 11:12
  • here https://developers.google.com/maps/documentation/static-maps/error-messages says that **The API key included in the request is invalid. 403 FORBIDDEN** check this. – Dharmishtha Dec 12 '17 at 11:13