Question

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. 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.

I found the error. It was that I did not have the permission in the android manifest!

Was it helpful?

Solution

The call should be similar to:

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

or as stated in the API,

GET http://api.wunderground.com/api/Your_Key/features/settings/q/query.format
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top