NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

Hello,

I'm trying to geocode some addresses querying Nominatim. If I enter one address like https://nominatim.openstreetmap.org/search.php?q=31+muntaner%2C+Barcelona%2C+Spain&polygon_geojson=1&viewbox=&format=xml, any web browser runs OK and I get the results.

But when trying to do the same from a Java class:

URL u;
InputStream is = null;

u = new URL("https://nominatim.openstreetmap.org/search.php?q=31+muntaner%2C+Barcelona%2C+Spain&polygon_geojson=1&viewbox=&format=xml");

is = u.openStream();

I always get a ConnectionTimedOut exception, and no results given.

Has anyone tried to do something similar with success? The timeout exception could be due to the absence on UsertAgent headers into the request? If so, what UserAgents are valid?

If somebody could post some code to query Nominatim from Java, I will greatly appreciate.

Thanks in advance, Joan.

asked 08 Jan '19, 21:22

Joan%20Segura's gravatar image

Joan Segura
26224
accept rate: 0%

edited 09 Jan '19, 07:23

scai's gravatar image

scai ♦
33.3k21309459


URL openStream does exactly what the name says, it returns an InputStream (over the underlying TCP connection) to the server in question. It doesn't do any of the bits and pieces of the HTTP protocol (which why you are simply seeing timeouts) and except if you want to do that yourself you are using the the wrong classes and methods.

Have a look at https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html

PS: ~~nowdays you would normally use the OkHttp library but as long as performance is not a concern there is no problem with being old fashioned.~~ OkHttp can no longer be recommended

permanent link

answered 09 Jan '19, 15:55

SimonPoole's gravatar image

SimonPoole ♦
44.7k13326701
accept rate: 18%

edited 04 Apr '19, 18:37

The Java library likely uses a default user agent string. Set it to something unique. See second requirement on https://operations.osmfoundation.org/policies/nominatim/

permanent link

answered 08 Jan '19, 22:03

mtmail's gravatar image

mtmail
4.8k1574
accept rate: 27%

Thanks for your answer mtmail.

I'm not using the osmapi from westnordost because when I've copied it into my project appeared a lot of dependencies I hadn't been able to solve. I was trying to open the URL directly.

Best regards. Joan.

(08 Jan '19, 22:20) Joan Segura

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×689
×133
×76

question asked: 08 Jan '19, 21:22

question was seen: 3,746 times

last updated: 04 Apr '19, 18:37

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum