[英]Unknown host exception, Apache HttpClient, Java, wunderground
我一直在這個特殊的困境上停留了一段時間,我已經搜尋了該網站並找到了一些幫助,但是並沒有解決我的特定問題。 我正在嘗試連接到網站以從中提取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
都會收到“ Unknown host exception
。
我發現了錯誤。 那是我在android清單中沒有權限!
該呼叫應類似於:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.