我一直坚持这个特殊的困境已有一段时间了,我已经搜索了网站并找到了一些帮助,但不是我的特定问题。我正在尝试连接到网站以从中提取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.com
或api.wunderground.com
,但当我尝试其中任何一个时,我都会Unknown host exception
。
我发现了错误。是因为我没有在android清单中获得许可!
答案 0 :(得分:0)
呼叫应类似于:
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