我正在尝试使用这个简单的代码获取 API 数据:
while (true)
{
BufferedReader br = new BufferedReader(new InputStreamReader(new
URL("https://api.darksky.net/forecast/f34171a387dbb5f4a29c66f6adc52e2f /49.133333,6.16667")
.openStream()));
String temp = br.readLine();
MyAPI = new JSONObject(strTemp);
System.out.println(MyAPI.getJSONObject("currently").getLong("time"));
br.close();
Thread.sleep(5000);
}
通常情况下,我每次请求 API 时得到的“时间”都应该不同。
当我使用网络浏览器请求 API 时没问题,但使用我的 java 应用程序我总是整天收到相同的数据。VP
我错过了什么吗?
最佳答案
您的网址
URL("https://api.darksky.net/forecast/f34171a387dbb5f4a29c66f6adc52e2f /49.133333,6.16667")
假设没有空格
URL("https://api.darksky.net/forecast/f34171a387dbb5f4a29c66f6adc52e2f/49.133333,6.16667")
关于JAVA获取URL API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40093409/