I am trying to get some data from openweathermap.org via java, but when I run the code I get a ConnectionException.
我试图通过java从openweathermap.org获取一些数据,但是当我运行代码时,我得到一个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)
奇怪的是,当我在网络浏览器中调用此链接时,我得到了预期的数据。怎么可能?我在这里想念的是什么?我该如何解决? (我尝试了所有三个uri,都在firefox中工作,而不是在我的程序中)
2 个解决方案
#1
1
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.
答案很简单:我的工作计算机在代理后面,只有firefox使用它。通过一点代理魔术,我终于能够获得预期的结果。
Thanks Tom and Jim Garrison for their usefull comments!
感谢Tom和Jim Garrison的有用评论!
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));
}
#2
-1
I was facing the same issue. Mine got fixed when i re-checked my JAVA installation. I had both JDK 6 and 7.. this kind of messed up .. so i removed jdk 7 completely and pointed to jdk 6. So command line version , eclipse and server all should point to same jdk.. this resolved the error.
我面临同样的问题。当我重新检查我的JAVA安装时,我得到了修复。我有JDK 6和7 ..这种搞砸..所以我完全删除了jdk 7并指向jdk 6.所以命令行版本,eclipse和服务器都应该指向相同的jdk ..这解决了错误。
#1
1
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.
答案很简单:我的工作计算机在代理后面,只有firefox使用它。通过一点代理魔术,我终于能够获得预期的结果。
Thanks Tom and Jim Garrison for their usefull comments!
感谢Tom和Jim Garrison的有用评论!
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));
}
#2
-1
I was facing the same issue. Mine got fixed when i re-checked my JAVA installation. I had both JDK 6 and 7.. this kind of messed up .. so i removed jdk 7 completely and pointed to jdk 6. So command line version , eclipse and server all should point to same jdk.. this resolved the error.
我面临同样的问题。当我重新检查我的JAVA安装时,我得到了修复。我有JDK 6和7 ..这种搞砸..所以我完全删除了jdk 7并指向jdk 6.所以命令行版本,eclipse和服务器都应该指向相同的jdk ..这解决了错误。