Execute get request with Apache Components

This example will show how to make a get request using Apache HttpComponents httpclient. In the event you are using Commons HttpClient, you will want to upgrade as it is no longer being developed. This approach is pretty low level and most frameworks wrap this code to make deserializing responses to java object simpler.

SparkFun allows you to create a dynamic feed or stream of data. Exploring the public feed we will make a request to Wimp Weather Station returning a response content type of JSON.

@Test
public void make_request_http_client() throws ClientProtocolException,
        IOException {

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(
            "https://data.sparkfun.com/streams/dZ4EVmE8yGCRGx5XRX1W.json");
    CloseableHttpResponse response = httpclient.execute(httpget);
    try {
        HttpEntity entity = response.getEntity();

        if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
            String responseValue = EntityUtils.toString(entity);
            System.out.println(responseValue);
        } else {
            // log error
        }
    } finally {
        response.close();
    }
}

Output

{
  "success":true,
  "publicKey":"dZ4EVmE8yGCRGx5XRX1W",
  "stream":{
    "_events":{

    },
    "_posts":{
      "save":[

      ]
    },
    "_pres":{
      "save":[
        null,
        null,
        null
      ]
    },
    "_doc":{
      "tags":[
        "battery",
        "humidity",
        "light",
        "pressure",
        "rain",
        "temperature",
        "weather",
        "wind"
      ],
      "fields":[
        "baromin",
        "batt_lvl",
        "dailyrainin",
        "dewptf",
        "humidity",
        "light_lvl",
        "measurementTime",
        "rainin",
        "tempf",
        "winddir",
        "winddir_avg2m",
        "windgustdir",
        "windgustdir_10m",
        "windgustmph",
        "windgustmph_10m",
        "windspdmph_avg2m",
        "windspeedmph"
      ],
      "date":"2014-04-05T14:37:39.441Z",
      "last_push":"2014-09-12T00:20:43.760Z",
      "hidden":false,
      "flagged":false,
      "alias":"wimp_weather",
      "location":{
        "long":"Boulder, CO, United States",
        "city":"Boulder",
        "state":"Colorado",
        "country":"United States",
        "lat":"40.0149856",
        "lng":"-105.27054559999999"
      },
      "title":"Wimp Weather Station",
      "description":"Weather station on top my roof. Read the full tutorial here: https://learn.sparkfun.com/tutorials/weather-station-wirelessly-connected-to-wunderground",
      "__v":0,
      "_id":"534015331ebf49e11af8059d"
    },
    "_maxListeners":0,
    "isNew":false,
    "$__":{
      "strictMode":false,
      "getters":{

      },
      "wasPopulated":false,
      "activePaths":{
        "paths":{
          "flagged":"init",
          "hidden":"init",
          "last_push":"init",
          "date":"init",
          "fields":"init",
          "title":"init",
          "tags":"init",
          "alias":"init",
          "location":"init",
          "description":"init",
          "__v":"init",
          "_id":"init"
        },
        "states":{
          "ignore":{

          },
          "default":{

          },
          "init":{
            "alias":true,
            "location":true,
            "flagged":true,
            "hidden":true,
            "title":true,
            "tags":true,
            "last_push":true,
            "fields":true,
            "description":true,
            "date":true,
            "__v":true,
            "_id":true
          },
          "modify":{

          },
          "require":{

          }
        },
        "stateNames":[
          "require",
          "modify",
          "init",
          "default",
          "ignore"
        ]
      }
    }
  }
}