0

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Enzo
  • 57
  • 9

1 Answers1

0

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?

scai
  • 20,297
  • 4
  • 56
  • 72
  • I think you are right, how do i provide a valid HTTP user agent? – Enzo Mar 12 '19 at 02:17
  • According to [this](https://stackoverflow.com/q/2529682/1340631) you can call `setRequestProperty("User-Agent", "your user agent")` on your `URLConnection` object. Be sure to set a user agent that is unique to your application. – scai Mar 12 '19 at 07:45