This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Add a new data model object to the project to handle the daily weather forecast.
Additional Resources
- Register with the Dark Sky API
- JSON Viewer for the Chrome Browser
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
As I mentioned, we'll continue to use
the Dark Sky API to get weather data for
0:00
this project.
0:04
The response we get from this API
gives us a list of hourly forecast for
0:06
the next two days, which is
perfect as it's just what we need.
0:10
We're already getting this
data in the JSON response.
0:14
We just haven't accessed it yet.
0:17
Let's start by adding this
new data to our data model.
0:20
Let's take a look at the JSON data
we get from the Dark Sky API.
0:24
If you don't already have an account,
0:28
sign up at darksky.net/dev/register for
a free account.
0:32
Once you've logged in, we want to
click on the personal test URL here
0:37
in the dashboard, to view weather data for
Alcatraz Island in California.
0:41
Now that I'm using an extension for
0:50
the chrome browser, called JSON viewer,
to make this JSON data look a bit nicer.
0:52
It's linked in the teachers notes,
0:57
our stormy app is currently using
the data from this currently object.
1:00
We now want to add hourly forecast.
1:08
As luck will have it, there's
an array of hourly forecast objects.
1:11
There's a lot here, but we'll just
pick a few key pieces of information.
1:16
Remember, if you want more details
about any of the data in this API,
1:21
we can head back to the developer
dashboard, Click on documentation,
1:25
and The Response Format, where we can
read about all the different data points.
1:34
Let's go back and
look at our hourly forecast object again.
1:41
So here we see,
we have an array of JSON objects,
1:51
as designated by the square bracket.
1:54
This makes for a nice and
direct translation.
1:56
We'll use an array of Java
objects inside our data model.
1:59
Each object will be an hour
item that will hold the time,
2:04
a short summary of the weather conditions,
the temperature, and the weather icon.
2:07
Let's head to Android Studio and
get started.
2:13
We need to add to the apps
data model to hold this data.
2:17
Recall that we're following
a model view controller, or
2:20
MVC design pattern, where the model
2:23
is classes that represent the data we're
getting from the the Dark Sky API.
2:25
Our project currently has one class.
2:30
We go to app Java.
2:32
And here,
our model class is current weather.
2:34
We'll be getting more data,
and we'll add another class.
2:38
Since we're gonna have a few models, let's
make a new subpackage in our project.
2:41
We'll call it weather,
to store our model classes.
2:46
I'm making refactored current
weather into our new package.
2:52
Let's create a new class to
hold the data for each hour.
3:05
Then, we can create an array of these
3:08
hour items to hold the whole
hourly forecast array.
3:11
So we can make a new hour class.
3:14
I always have trouble naming classes,
but we can always refactor later on,
3:21
if needed.
3:25
So now for our class variables,
based on what we're using from
3:29
our hourly forecast object,
we'll need a time, private long time.
3:34
Recall that the time value we are getting
in from dark sky, is a Unix time value.
3:40
We'll need our summary, which is a string.
3:46
We'll need our temperature,
which was a double value.
3:51
And we'll need our icon,
which was another string.
3:57
We're going to need one additional
piece of data later on, too.
4:03
For convenience, let's add it now, so we
don't forget, or get stuck down the road.
4:07
That piece is the timezone, so
4:12
that we will be able to display
the hour appropriately.
4:14
And we'll definitely need our getters and
setters for these.
4:23
So let's let Android Studio
generate those for us!
4:26
Code > Generate.
4:32
Getters and Setters, we want all of them.
4:34
Now we have two different model classes,
current weather and hour.
4:39
But, we need a way to group them together.
4:43
We don't yet
have a way to hold the hourly forecast.
4:45
Our solution, is to again model our
java classes after the data in the API.
4:49
We're getting data as a response
from the Dark Sky server, and
4:55
it's all bundled together
in one big JSONObject.
4:58
Let's create our own object to
bundle up our model classes.
5:02
So Weather > New > Class.
5:07
We'll call it Forecast, and
5:11
it will have a current weather object and
an array of hour items.
5:13
Call it Forecast.
5:17
So inside here,
we want a private CurrentWeather,
5:22
we'll call it currentWeather.
5:28
We want an array of hour objects,
we'll call it hourlyForecast,
5:34
And generate our Getters and Setters.
5:43
Now that we're in here, I don't much
like the name CurrentWeather anymore.
5:50
Now that it's all part
of this forecast object.
5:54
Let's refactor that class
name to just be current.
5:57
Refactor, rename, we will call
6:00
it Current, and do the refactor.
6:05
We want to select all of them, and hit OK.
6:13
All right, our models are looking great.
6:23
Now that we have a separate package for
our models, let's create a new subpackage
6:25
called UI for these other classes,
and move them into it.
6:29
New > Package > UI.
6:34
I'll move our UI elements
into our UI package.
6:37
Sometimes, when we move activities
from one package to another,
6:44
it can cause an error
inside the manifest file.
6:48
Let's check in there and take a look.
6:51
Sometimes, the Android name attribute for
each activity element, isn't updated.
6:57
It should be fine like it is in here, but
if your app crashes after a refactoring
7:03
package for activities, you may need
to track down the error inside here.
7:08
Let's take a quick break, and tackle
populating the daily weather forecast.
7:13
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up