Search code examples
javaandroidjsonopenstreetmapnominatim

Can't get JSON from nominatim url (openstreetmap)


I cannot get the result from the URL JSON response of reverse geocoding. I'm also getting the error:

"W/System.err: org.json.JSONException: End of input at character 0 of "

Here is the url https://nominatim.openstreetmap.org/reverse?format=geojson&lat=14.6458&lon=121.0949

HttpDataHandler.java

public String GetHTTPData(String requestUrl)
{
    URL url;
    String response = "";
    try{
        url = new URL(requestUrl);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        conn.setDoOutput(true);
        int responseCode = conn.getResponseCode();

        if(responseCode == HttpURLConnection.HTTP_OK)
        {
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while((line = br.readLine()) != null)
                response+=line;
        }
        else
            response = "";

MainActivity

 @Override
    protected String doInBackground(String... strings) {
        try{
            double lat = Double.parseDouble(strings[0].split(",")[0]);
            double lng = Double.parseDouble(strings[0].split(",")[1]);
            String response;
            HttpDataHandler http = new HttpDataHandler();
            //String url = String.format("http://open.mapquestapi.com/geocoding/v1/reverse?key=2KtQuwfGGdfHxj6ybdgqcC7uFHrgVoJy&location=%.4f,%.4f",lat,lng);
            String url = String.format("https://nominatim.openstreetmap.org/reverse?format=json&lat=%.4f&lon=%.4f",lat,lng);
            response = http.GetHTTPData(url);
            Log.d("testpandebug,doinbg", response);
            Log.d("testpandebug,doinbg", url);
            return response;
        }
        catch (Exception ex)
        {

        }
        return null;
    }

I'm using logs to get the response from the url yet. Upon using the other geocoder URL (commented string URL), it returns a response, however, using the nominatim doesnt return a response. I want to use the nominatim as it returns a more accurate reverse geocode.


Solution

  • If it doesn't return a valid body then take a look at the HTTP response header. I guess you are violating Nominatim's usage policy. Do you provide a valid HTTP user agent?