Getting the Whole Forecast4:54 with Ken Alger
We need to handle getting additional data from the Dark Sky API.
We're going to test the app at the end of this video. 0:00 So now would be a good time to pause me and 0:03 start your emulator if it isn't running already. 0:05 We're ready to fill our new data model object with data from the Dark Sky API. 0:09 We're setting the current weather in MainActivity. 0:14 Lets open it up and have a look. 0:17 So our MainActivity's here in the ui package now, MainActivity. 0:19 We have this getForecast method, which is using 0:24 the third party library OkHttp to get data from Dark Sky. 0:29 A successful request ends up calling the onResponse method, down here. 0:34 Line 82 shows that we are setting the current weather using a method called 0:42 getCurrentDetails. 0:47 We can scroll down to where it's declared, or 0:49 there are some keyboard shortcuts as well. 0:52 If you hold Cmd on a Mac or the Ctrl key on Windows and 0:55 click on the method name, Android Studio will jump to where the method is declared. 0:58 If you're interested in more keyboard shortcuts like this, 1:05 I've included links in the teachers notes. 1:08 The links are to the IntelliJ website, 1:11 which is the base tool that Android Studio is built upon. 1:13 The code inside getCurrentDetails methods here is using the JSONObject class 1:18 to convert the string of JSON data received from the API 1:23 into a Java object that we can access and manipulate. 1:27 We're filling up the current model object, 1:31 fortunately we can leave most of this code totally alone. 1:34 But we now want to fill up the whole forecast object and 1:38 not just the current object. 1:41 Let's add a new method to do just that. 1:43 And in this new method, we can call getCurrentDetails, 1:45 as well as new methods to get the hourly forecast details. 1:49 So above getCurrentDetails, let's add some space. 1:53 Our new method will be private. 1:57 It will return a Forecast. 2:00 Let's call it parseForecastData. 2:02 And it'll take a string of jsonData. 2:06 Now, first, we need a Forecast variable. 2:12 Forecast, we'll call it Forecast, and it'll be new Forecast. 2:16 Once we get done building that object, this is what we'll return. 2:23 Before we forget, let's return it. 2:27 Our forecast object has a member variable for the current forecast. 2:33 We generated a setter method for it inside our model. 2:38 So we can set that up here. 2:42 Forecast.setCurrent, which takes a current object. 2:45 And in here we can just call the getCurrentDetails method directly, 2:50 since it returns a current object for us, getCurrentDetails. 2:55 Now we'll pass along the same jsonData stream we're using for our method And 2:59 we get an unhandled JSON exception error because getCurrentDetails throws one. 3:06 Let's throw it again, as we're going to call this new method 3:12 from the same place we were previously calling getCurrentDetails. 3:15 We can use the quick fix to add throws JSONException to this method. 3:19 Let's update our code now to set the whole forecast instead of just the current 3:26 weather conditions. 3:30 We will go all the way back up to the top. 3:31 We'll replace this current variable with a forecast. 3:37 Forecast forecast, and there we go. 3:41 And now onto the error hunt to fix things. 3:46 We can either scroll down to the new error or 3:48 click on it over here on the right-hand side. 3:52 This line here, line 83, is taken care of inside our new method. 3:55 We can replace it with our forecast. 4:00 So forecast, And we use the same jsonData string. 4:03 But now instead of getCurrentDetails, we want parseForecastData. 4:11 There are a few more errors here caused by removing the current member variable. 4:15 We go down to those and 4:20 we simply need to access this through the forecast variable now. 4:21 Let's add a new variable here just to make it a little bit easier. 4:25 So Current, our variable name down there is current. 4:32 And that'll be forecast and getCurrent. 4:37 Let's test it out Very nice. 4:42 All of our data is still being displayed. 4:51
You need to sign up for Treehouse in order to download course files.Sign up