我想通过java从openweathermap.org获取一些数据,但是当我运行代码时,我得到一个ConnectionException。为什么我的连接被拒绝?
我的代码是:
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);
}
和例外:
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)
奇怪的是,当我在我的web浏览器中调用this link,我得到预期的数据。怎么可能?我在这里错过了什么?我该如何解决它? (我尝试了所有三个uri,都在Firefox中工作,而不是在我的程序中)
你有没有检查是否有防火墙? – ADTC
我编译你的代码,它工作正常(呃,实际上它提取JSON而不是XML,但它只是当给定一个'text/xml'或'application/xml'' Accept'头时该服务是如何工作的)。问题在于别处。你有没有防火墙运行? – toniedzwiedz
谢谢汤姆。我不明白的是,为什么我可以用firefox连接而不用java连接。他们使用不同的端口吗?我明天会检查我的系统管理员,因为我确实可以控制防火墙。 – Burkhard