未知的主机异常,Apache HttpClient,Java,wunderground

时间:2013-04-09 14:02:21

标签: java android url apache-httpclient-4.x unknown-host

我一直坚持这个特殊的困境已有一段时间了,我已经搜索了网站并找到了一些帮助,但不是我的特定问题。我正在尝试连接到网站以从中提取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,但当我尝试其中任何一个时,我都会Unknown host exception

我发现了错误。是因为我没有在android清单中获得许可!

1 个答案:

答案 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