1

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!

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rapitor
  • 176
  • 12
  • Can you send a ping request? Maybe something is wrong with you local installation. – sk2212 Apr 09 '13 at 14:16
  • hmm, that sounds like it could be something. I can access the website fine. but pinging it returns "timed out" indefenitely – Rapitor Apr 09 '13 at 14:22
  • your code works fine for me with a little addition: your **urllink** should begin with a protocol: **http://api.wunderground.com/api/Your_Key/features/settings/q/query.format**. Also, I removed unnessecary targetHost and replaced **client.execute(targetHost, httpGet);** with just **client.execute(httpGet);** – Alex Apr 09 '13 at 14:42
  • I'm still getting unknown host. I guess that means I'm behind a firewall or proxy? solution? – Rapitor Apr 09 '13 at 15:01

1 Answers1

0

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
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61