简体   繁体   English

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

[英]Unknown host exception, Apache HttpClient, Java, wunderground

I've been stuck on this particular dilemma for some time, I have scoured the site and found some help, but not to my particular issue. 我一直在这个特殊的困境上停留了一段时间,我已经搜寻了该网站并找到了一些帮助,但是并没有解决我的特定问题。 I'm trying to connect to a website to extract JSON data from it. 我正在尝试连接到网站以从中提取JSON数据。 The host is what i'm not sure about: 主机是我不确定的是:

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();

The host could either be www.wunderground.com or api.wunderground.com , but when I try either of them i get Unknown host exception . 主机可以是www.wunderground.comapi.wunderground.com ,但是当我尝试使用其中任何一个Unknown host exception都会收到“ Unknown host exception

I found the error. 我发现了错误。 It was that I did not have the permission in the android manifest! 那是我在android清单中没有权限!

The call should be similar to: 该呼叫应类似于:

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

or as stated in the API , 或如API中所述,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM