Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App (2015) Working with JSON Setting CurrentWeather from JSON

How to change the degree to Celsius?

Hi I just want to know in the "Stormy App" How I can change the temperature in which file of the project? Thanks

1 Answer

Hi Ali,

The temperature is held in the CurrentWeather class instance called currentWeather. There's a getter method that returns the value provided by the JSON data. At the moment, that's in Fahrenheit; you want a conversion to Celsius, I assume?

At the moment, the method is:

public int getTemperature() {
   return (int)Math.round(mTemperature);
}

So, you need to tinker with mTemperature before it is returned. Converting from F to C involves subtracting 32, then multiplying the result by 5/9. (I think!!)

So, something like:

public int getTemperature() {
   return (int)Math.round((mTemperature -32) * (5/9));
}

That might do the trick. I hope that helps and works!!

Steve.

Good idea.

Brendan Milton
Brendan Milton
9,050 Points

Hi Steve!

I read over your info on converting from F to C, Im just struggling with how to use the method in the current weather program? would it be used in conjunction with one of the following lines of code?

ie: "current.setTemperature(currently.getDouble("temperature"));" Towards the bottom?

  private Current getCurrentDetails(String jsonData) throws JSONException {
        JSONObject forecast = new JSONObject(jsonData);
        String timezone = forecast.getString("timezone");
        Log.i(TAG, "From JSON: " + timezone);

        JSONObject currently = forecast.getJSONObject("currently");

        Current current = new Current();
        current.setHumidity(currently.getDouble("humidity"));
        current.setTime(currently.getLong("time"));
        current.setIcon(currently.getString("icon"));
        current.setPrecipChance(currently.getDouble("precipProbability"));
        current.setSummary(currently.getString("summary"));
        current.setTemperature(currently.getDouble("temperature"));
        current.setTimeZone(timezone);


        Log.d(TAG, current.getFormattedTime());

        return current;
    }

Hi Brendan Milton

I implemented the conversion in the class definition. So, inside the Class definition for, say, Current, I have:

public class Current {
  .
  .
  .
  private double mTemperature;
  .
  .
  .

  public int getTemperature() {
    double celsius = (mTemperature - 32.0) * 0.5555;
    return (int)Math.round(celsius);
  }
}

I have the same in the Hour class. Within the Day class, I applied the maths within the getTemperatureMax() method.

Yes, you can use fewer lines of code just return the calculation, but I prefer the more verbose approach as it makes the calculation clearer, for me.

I hope that makes sense.

Steve.