1

一段时间以来,我一直陷入这个特殊的困境,我搜索了该站点并找到了一些帮助,但不是针对我的特定问题。我正在尝试连接到一个网站以从中提取 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清单中没有权限!

4

1 回答 1

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
于 2013-04-09T14:15:08.140 回答