我正在尝试创建一个连接到API的应用程序。 Rigth现在我正在尝试连接到API并打印出结果。但是,出于某种原因,我不断收到以下错误:
Exception in thread "main" java.net.UnknownHostException: www.data.goteborg.se
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at TestAPI.getHTML(TestAPI.java:14)
at TestAPI.main(TestAPI.java:29)
为什么会发生这种情况?贝娄是我的代码。提前致谢。
import java.io.*;
import java.net.*;
//import org.json.JSONObject;
public class TestAPI {
public static String getHTML(String urlToRead) throws Exception {
StringBuilder result = new StringBuilder();
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString(); // JSON Object
}
public static void main(String[] args) throws Exception {
System.out.println("Test1");
String data;
data = getHTML("http://data.goteborg.se/AirQualityService/v1.0/LatestMeasurement/"
+ "myAPIkey?format=Json");
System.out.println(data);
}
}
答案 0 :(得分:0)
尝试连接www.data.goteborg.se。 我认为您正在处理dns查找失败。
在this link中,您可以找到3种方法来修复 DNS_PROBE_FINISHED_NXDOMAIN 错误。