Skip to content

Instantly share code, notes, and snippets.

@sax1johno
Created February 23, 2016 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sax1johno/62147a5e2b7d9d9b5f10 to your computer and use it in GitHub Desktop.
Save sax1johno/62147a5e2b7d9d9b5f10 to your computer and use it in GitHub Desktop.
URL requestUrl = new URL(fullURLRequest);
HttpUrlConnection urlConnection = (HttpURLConnection) requestUrl.openConnection();
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder jsonString = new StringBuilder();
// You only want to call readLine once - the assignment will fail once readLine is null;
// calling ReadLine twice will cause an error.
while (String line = bufferedReader.readLine()) {
jsonString.append(line);
}
JSONObject obj = new JSONObject(jsonString);
WeatherUnderGroundData weatherData = new WeatherUnderGroundData();
weatherData.fromJson(obj);
{
"response": {
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1,
"astronomy": 1
}
},
"current_observation": {
"image": {
"url": "http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title": "Weather Underground",
"link": "http://www.wunderground.com"
},
"display_location": {
"full": "Oakland, CA",
"city": "Oakland",
"state": "CA",
"state_name": "California",
"country": "US",
"country_iso3166": "US",
"zip": "94609",
"magic": "1",
"wmo": "99999",
"latitude": "37.830000",
"longitude": "-122.270000",
"elevation": "31.00000000"
},
"observation_location": {
"full": "Longfellow, Oakland, California",
"city": "Longfellow, Oakland",
"state": "California",
"country": "US",
"country_iso3166": "US",
"latitude": "37.832680",
"longitude": "-122.274101",
"elevation": "63 ft"
},
"estimated": {},
"station_id": "KCAOAKLA43",
"observation_time": "Last Updated on February 22, 9:22 PM PST",
"observation_time_rfc822": "Mon, 22 Feb 2016 21:22:26 -0800",
"observation_epoch": "1456204946",
"local_time_rfc822": "Mon, 22 Feb 2016 21:22:31 -0800",
"local_epoch": "1456204951",
"local_tz_short": "PST",
"local_tz_long": "America/Los_Angeles",
"local_tz_offset": "-0800",
"weather": "Mostly Cloudy",
"temperature_string": "51.7 F (10.9 C)",
"temp_f": 51.7,
"temp_c": 10.9,
"relative_humidity": "94%",
"wind_string": "Calm",
"wind_dir": "NNE",
"wind_degrees": 22,
"wind_mph": 0,
"wind_gust_mph": 0,
"wind_kph": 0,
"wind_gust_kph": 0,
"pressure_mb": "1020",
"pressure_in": "30.12",
"pressure_trend": "0",
"dewpoint_string": "50 F (10 C)",
"dewpoint_f": 50,
"dewpoint_c": 10,
"heat_index_string": "NA",
"heat_index_f": "NA",
"heat_index_c": "NA",
"windchill_string": "NA",
"windchill_f": "NA",
"windchill_c": "NA",
"feelslike_string": "51.7 F (10.9 C)",
"feelslike_f": "51.7",
"feelslike_c": "10.9",
"visibility_mi": "10.0",
"visibility_km": "16.1",
"solarradiation": "--",
"UV": "0",
"precip_1hr_string": "0.00 in ( 0 mm)",
"precip_1hr_in": "0.00",
"precip_1hr_metric": " 0",
"precip_today_string": "0.00 in (0 mm)",
"precip_today_in": "0.00",
"precip_today_metric": "0",
"icon": "mostlycloudy",
"icon_url": "http://icons.wxug.com/i/c/k/nt_mostlycloudy.gif",
"forecast_url": "http://www.wunderground.com/US/CA/Oakland.html",
"history_url": "http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KCAOAKLA43",
"ob_url": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=37.832680,-122.274101",
"nowcast": ""
},
"moon_phase": {
"percentIlluminated": "100",
"ageOfMoon": "15",
"phaseofMoon": "Full",
"hemisphere": "North",
"current_time": {
"hour": "21",
"minute": "22"
},
"sunrise": {
"hour": "6",
"minute": "50"
},
"sunset": {
"hour": "17",
"minute": "55"
},
"moonrise": {
"hour": "18",
"minute": "11"
},
"moonset": {
"hour": "6",
"minute": "41"
}
},
"sun_phase": {
"sunrise": {
"hour": "6",
"minute": "50"
},
"sunset": {
"hour": "17",
"minute": "55"
}
}
}
public class WeatherUnderGroundData {
private String weatherType;
private String temp_f;
private String temp_c;
private String wind_mph;
private String wind_kph;
private String display_location;
private String sunriseHour;
private String sunriseMinute;
private String percentIlluminated;
public WeatherUnderGroundData() {
}
public void fromJson(JSONObject obj) {
this.weatherType = obj.current_observation.weather;
this.temp_f = obj.current_observation.temp_f;
this.temp_c = obj.current_observation.temp_c;
// etc
}
}
@sax1johno
Copy link
Author

For ConnectionSnippet.java:
String line = bufferedReader.readLine();
while (line != null ) {
jsonString.append(line);
line = bufferedReader.readLine();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment