I am trying to get some data from openweathermap.org via java, but when I run the code I get a ConnectionException.
My code is:
public static void openweathermapTest1() {
String uri = "http://openweathermap.org/data/2.1/find/station?lat=55&lon=37&cnt=10";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(uri);
String xml = service.accept(MediaType.TEXT_XML).get(String.class);
System.out.println("Output as XML: " + xml);
}
and the Exception:
Exception in thread "main"
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151)
at com.sun.jersey.api.client.Client.handle(Client.java:648)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
at GetPoint.openweathermapTest1(GetPoint.java:110)
at GetPoint.main(GetPoint.java:142)
Strangely, when I call this link in my web browser, I get the expected data. How can that be? What am I missing here? And how can I fix it? (I tried all three uri and all worked in firefox and not in my programm)
The answer is quite simple: my work computer is behind a proxy and only firefox was using it. With a little proxy magic I was finally able to get the expected result.
Thanks Tom and Jim Garrison for their usefull comments!
Edit: I used the following code to use the proxy:
private static void useProxy(String host, int port)
{
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", String.valueOf(port));
}