我想通過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