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.