1 00:00:00,400 --> 00:00:03,120 We're going to test the app at the end of this video. 2 00:00:03,120 --> 00:00:05,490 So now would be a good time to pause me and 3 00:00:05,490 --> 00:00:09,150 start your emulator if it isn't running already. 4 00:00:09,150 --> 00:00:13,600 We're ready to fill our new data model object with data from the Dark Sky API. 5 00:00:14,660 --> 00:00:17,430 We're setting the current weather in MainActivity. 6 00:00:17,430 --> 00:00:19,817 Lets open it up and have a look. 7 00:00:19,817 --> 00:00:24,670 So our MainActivity's here in the ui package now, MainActivity. 8 00:00:24,670 --> 00:00:29,024 We have this getForecast method, which is using 9 00:00:29,024 --> 00:00:34,133 the third party library OkHttp to get data from Dark Sky. 10 00:00:34,133 --> 00:00:38,352 A successful request ends up calling the onResponse method, down here. 11 00:00:42,952 --> 00:00:47,095 Line 82 shows that we are setting the current weather using a method called 12 00:00:47,095 --> 00:00:48,335 getCurrentDetails. 13 00:00:49,630 --> 00:00:52,660 We can scroll down to where it's declared, or 14 00:00:52,660 --> 00:00:55,290 there are some keyboard shortcuts as well. 15 00:00:55,290 --> 00:00:58,960 If you hold Cmd on a Mac or the Ctrl key on Windows and 16 00:00:58,960 --> 00:01:05,690 click on the method name, Android Studio will jump to where the method is declared. 17 00:01:05,690 --> 00:01:08,970 If you're interested in more keyboard shortcuts like this, 18 00:01:08,970 --> 00:01:11,260 I've included links in the teachers notes. 19 00:01:11,260 --> 00:01:13,720 The links are to the IntelliJ website, 20 00:01:13,720 --> 00:01:16,670 which is the base tool that Android Studio is built upon. 21 00:01:18,160 --> 00:01:23,310 The code inside getCurrentDetails methods here is using the JSONObject class 22 00:01:23,310 --> 00:01:27,550 to convert the string of JSON data received from the API 23 00:01:27,550 --> 00:01:31,660 into a Java object that we can access and manipulate. 24 00:01:31,660 --> 00:01:34,370 We're filling up the current model object, 25 00:01:34,370 --> 00:01:38,250 fortunately we can leave most of this code totally alone. 26 00:01:38,250 --> 00:01:41,027 But we now want to fill up the whole forecast object and 27 00:01:41,027 --> 00:01:43,350 not just the current object. 28 00:01:43,350 --> 00:01:45,890 Let's add a new method to do just that. 29 00:01:45,890 --> 00:01:49,330 And in this new method, we can call getCurrentDetails, 30 00:01:49,330 --> 00:01:52,190 as well as new methods to get the hourly forecast details. 31 00:01:53,290 --> 00:01:57,732 So above getCurrentDetails, let's add some space. 32 00:01:57,732 --> 00:02:00,125 Our new method will be private. 33 00:02:00,125 --> 00:02:02,170 It will return a Forecast. 34 00:02:02,170 --> 00:02:04,230 Let's call it parseForecastData. 35 00:02:06,980 --> 00:02:09,156 And it'll take a string of jsonData. 36 00:02:12,628 --> 00:02:14,350 Now, first, we need a Forecast variable. 37 00:02:16,700 --> 00:02:20,069 Forecast, we'll call it Forecast, and it'll be new Forecast. 38 00:02:23,185 --> 00:02:27,690 Once we get done building that object, this is what we'll return. 39 00:02:27,690 --> 00:02:29,989 Before we forget, let's return it. 40 00:02:33,928 --> 00:02:38,300 Our forecast object has a member variable for the current forecast. 41 00:02:38,300 --> 00:02:41,000 We generated a setter method for it inside our model. 42 00:02:42,130 --> 00:02:43,513 So we can set that up here. 43 00:02:45,557 --> 00:02:50,850 Forecast.setCurrent, which takes a current object. 44 00:02:50,850 --> 00:02:55,414 And in here we can just call the getCurrentDetails method directly, 45 00:02:55,414 --> 00:02:59,670 since it returns a current object for us, getCurrentDetails. 46 00:02:59,670 --> 00:03:06,631 Now we'll pass along the same jsonData stream we're using for our method And 47 00:03:06,631 --> 00:03:12,860 we get an unhandled JSON exception error because getCurrentDetails throws one. 48 00:03:12,860 --> 00:03:15,760 Let's throw it again, as we're going to call this new method 49 00:03:15,760 --> 00:03:18,740 from the same place we were previously calling getCurrentDetails. 50 00:03:19,750 --> 00:03:23,470 We can use the quick fix to add throws JSONException to this method. 51 00:03:26,040 --> 00:03:30,005 Let's update our code now to set the whole forecast instead of just the current 52 00:03:30,005 --> 00:03:31,155 weather conditions. 53 00:03:31,155 --> 00:03:32,608 We will go all the way back up to the top. 54 00:03:37,335 --> 00:03:39,820 We'll replace this current variable with a forecast. 55 00:03:41,270 --> 00:03:44,960 Forecast forecast, and there we go. 56 00:03:46,140 --> 00:03:48,934 And now onto the error hunt to fix things. 57 00:03:48,934 --> 00:03:52,272 We can either scroll down to the new error or 58 00:03:52,272 --> 00:03:55,920 click on it over here on the right-hand side. 59 00:03:55,920 --> 00:04:00,670 This line here, line 83, is taken care of inside our new method. 60 00:04:00,670 --> 00:04:03,461 We can replace it with our forecast. 61 00:04:03,461 --> 00:04:10,450 So forecast, And we use the same jsonData string. 62 00:04:11,470 --> 00:04:15,540 But now instead of getCurrentDetails, we want parseForecastData. 63 00:04:15,540 --> 00:04:20,090 There are a few more errors here caused by removing the current member variable. 64 00:04:20,090 --> 00:04:21,220 We go down to those and 65 00:04:21,220 --> 00:04:25,010 we simply need to access this through the forecast variable now. 66 00:04:25,010 --> 00:04:28,625 Let's add a new variable here just to make it a little bit easier. 67 00:04:32,110 --> 00:04:34,517 So Current, our variable name down there is current. 68 00:04:37,597 --> 00:04:40,387 And that'll be forecast and getCurrent. 69 00:04:42,410 --> 00:04:51,700 Let's test it out Very nice. 70 00:04:51,700 --> 00:04:53,450 All of our data is still being displayed.