private Current getCurrentDetails(String weatherData) throws JSONException { // Need a try catch block to fix the error when you first do this // OR WE CAN THROW AN EXCEPTION IN THE METHOD DECLARATION!! // ~ TO MAKE THE throws WORK, WE NEED TO CATCH THE EXCEPTION IN THE TRY CATCH BLOCK ABOVE IN THE // MAIN CLASS JSONObject forecast = new JSONObject(weatherData); // Creates a JSONObject instance to work with the JSON data String timezone = forecast.getString("timezone"); Log.i(TAG, "The timezone from JSON is: " + timezone); JSONObject currently = forecast.getJSONObject( "currently"); // Gets the whole JSON "currently" object for you to parse through Current current = new Current(); // Declares a Current object to set values for WITH THE JSON object's // (currently) data current.setHumidity(currently.getDouble("humidity")); current.setPrecipChance(currently.getDouble("precipProbability")); current.setIcon( currently.getString("icon")); // Retrieves the icon value for our mIcon in Current current.setSummary(currently.getString("summary")); current.setTemperature(currently.getDouble("temperature")); current.setTime( (currently.getLong( "time"))); // Pulling all these values from JSON keys and setting our Current's mVars current.setTimeZone(timezone); current.setWind((currently.getInt("windSpeed"))); Log.d(TAG, current.getFormattedTime()); return current; }
private void updateDisplay() { // This actually updates our display with the data we have parsed into the // Current class Current current = mForecast .getCurrent(); // Gets all of mCurrent's data in Forecast class so we can update them mTemperatureLabel.setText( current.getTemperature() + ""); // - NOTE: WE MUSHED THE INT VALUE WITH AN EMPTY STRING TO set the text mHumidityValue.setText(current.getHumidity() + ""); mTimeLabel.setText("At " + current.getFormattedTime() + " it will be"); mPrecipValue.setText(current.getPrecipChance() + "%"); mWindSpeed.setText(current.getWindSpeed() + "mph"); mSummaryLabel.setText(current.getSummary()); Drawable iconPic = getResources().getDrawable(current.getIconId()); mIconImageView.setImageDrawable(iconPic); }