I am developing an Android application where I use OpenStreetMap. through my location, I try to get the street address by sending coordinates to nominatim , but I end up receiving an "IOException: error 403" in the ImputStream. Also, if there is any other better way to obtain data from a URL?
URL
String requestString = "https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=" +
latitud + "&lon=" + longitud;
Asynctask
public static class getAddress extends AsyncTask<String,String,JSONObject>
{
@Override
protected JSONObject doInBackground(String... requestString) {
String jsonResponse = "";
String URL = requestString[0];
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL(URL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine = "";
while ((inputLine = reader.readLine()) != null)
{
buffer.append(inputLine);
}
jsonResponse = buffer.toString();
JSONObject response = new JSONObject(jsonResponse);
return response;
} catch (IOException e) {
Log.d("Error", "IOException: " + e.getMessage());
e.printStackTrace();
} catch (JSONException e) {
Log.d("Error", "JSONException: " + e.getMessage());
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
}
}
}
return null;
}
protected void onPostExecute(JSONObject response)
{
try
{
//Resto del código
}
catch (Exception e)
{
}
}
}