gpt4 book ai didi

java - 未知主机异常,Apache HttpClient,Java,wunderground

转载 作者:行者123 更新时间:2023-12-01 04:45:41 25 4
gpt4 key购买 nike

我已经陷入这个特殊的困境有一段时间了,我浏览了该网站并找到了一些帮助,但不是针对我的特定问题。我正在尝试连接到网站以从中提取 JSON 数据。主机是我不确定的:

DefaultHttpClient client = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("www.wunderground.com", 80);
HttpGet httpGet = new HttpGet(urllink); // urllink is "api.wunderground.com/api/my_key/conditions/forecast/hourly/alerts/q/32256.json"
httpGet.setHeader("Accept", "application/json");
httpGet.setHeader("Content-type", "application/json");

HttpResponse response = client.execute(targetHost, httpGet);

HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream));

StringBuilder stringBuilder = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
} catch (Exception e) {
// print stacktrace
return null;
} finally {
try {
instream.close();
} catch (Exception e) {
// print stacktrace
return null;
}
}

return stringBuilder.toString();

主机可以是www.wunderground.comapi.wunderground.com,但是当我尝试其中任何一个时,我得到未知主机异常.

我发现了错误。是我没有在android list 中获得权限!

最佳答案

调用应类似于:

http://api.wunderground.com/api/Your_Key/conditions/q/CA/San_Francisco.json

或如 API 中所述,

GET http://api.wunderground.com/api/Your_Key/features/settings/q/query.format

关于java - 未知主机异常,Apache HttpClient,Java,wunderground,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15904049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com