1 00:00:00,730 --> 00:00:04,010 Android provides some convenient ways to handle JSON data. 2 00:00:04,010 --> 00:00:06,130 Making it easy to work with. 3 00:00:06,130 --> 00:00:10,730 With our app connected to and receiving weather data in a JSON format. 4 00:00:10,730 --> 00:00:14,810 Let's take a look at how we can leverage the tools provided by Android to parse 5 00:00:14,810 --> 00:00:18,730 the weather data to get the information our app will use. 6 00:00:18,730 --> 00:00:21,850 We have our new data model object, CurrentWeather. 7 00:00:21,850 --> 00:00:23,480 Let's put it to use in main activity. 8 00:00:25,120 --> 00:00:29,658 So under our tag here, let's create a new, private, 9 00:00:29,658 --> 00:00:32,795 CurrentWeather object, and we'll call it currentWeather. 10 00:00:34,590 --> 00:00:38,640 Now, we'll want to set this, once we have a successful response. 11 00:00:38,640 --> 00:00:41,690 So we scroll down to our onResponse, and 12 00:00:41,690 --> 00:00:45,510 inside our if statement here where our response is successful. 13 00:00:46,790 --> 00:00:49,464 Let's set currentWeather. 14 00:00:49,464 --> 00:00:51,990 And we'll set it to a new method that we'll define here in a moment. 15 00:00:55,360 --> 00:00:56,970 We'll call it getCurrentDetails. 16 00:00:58,762 --> 00:01:01,830 We'll want to pass in JSON data. 17 00:01:01,830 --> 00:01:05,340 We can use the response.body that we have up here in our tag. 18 00:01:12,227 --> 00:01:16,480 But let's actually refactor that into a variable, and use it in both places. 19 00:01:20,692 --> 00:01:23,344 String.jsonData. 20 00:01:26,759 --> 00:01:29,940 Now we can pass in jsonData. 21 00:01:42,091 --> 00:01:47,988 Okay, now let's use the quick fix provided by Android Studio we put there. 22 00:01:47,988 --> 00:01:51,205 Alt + Enter, Create method. 23 00:01:51,205 --> 00:01:56,670 And again we want that inactivity not in our onCreate anonymous callback. 24 00:02:01,661 --> 00:02:05,900 Next, we're going to work with a special class called JSONObject. 25 00:02:05,900 --> 00:02:10,590 This class is able to hold any object represented in JSON format, and 26 00:02:10,590 --> 00:02:15,152 provides a few different methods to access the properties inside the JSON data. 27 00:02:15,152 --> 00:02:19,870 The JSONObject class has a constructor that lets us pass in a string 28 00:02:19,870 --> 00:02:23,620 of jsonData to create the new JSONObject. 29 00:02:23,620 --> 00:02:27,100 Fortunately, that's just what we have as a parameter. 30 00:02:27,100 --> 00:02:29,540 Let's create a new JSONObject inside our new method. 31 00:02:31,230 --> 00:02:37,850 JSONObject, plot forecast cuz that's what we're getting back. 32 00:02:41,556 --> 00:02:43,240 We see that we have an error here. 33 00:02:43,240 --> 00:02:44,800 We hover over it. 34 00:02:44,800 --> 00:02:48,480 We see that it is an unhandled exception. 35 00:02:48,480 --> 00:02:51,780 This is similar to the IOException we previously seen. 36 00:02:51,780 --> 00:02:54,030 But this is a JSONException. 37 00:02:54,030 --> 00:02:57,290 Just like we've done before, we can use the quick fix to add a dry catch. 38 00:02:59,460 --> 00:03:03,000 Now, while this works, this isn't ideal. 39 00:03:03,000 --> 00:03:05,670 It would be better if we could have the exception handle, or 40 00:03:05,670 --> 00:03:08,210 the getCurrentDetails method is called. 41 00:03:10,313 --> 00:03:13,220 Up here where the IOException is being caught. 42 00:03:13,220 --> 00:03:16,860 It would be great if we could get this new exception to bubble up, and 43 00:03:16,860 --> 00:03:18,150 be caught up here. 44 00:03:18,150 --> 00:03:20,900 We can do that with a throws keyword. 45 00:03:20,900 --> 00:03:27,450 Back here in getCurrentDetails, let's undo our try/catch block, 46 00:03:27,450 --> 00:03:34,790 and after our method signature before the curly brace, we wanna type throws. 47 00:03:34,790 --> 00:03:37,600 And then we need the specific type of exception. 48 00:03:37,600 --> 00:03:42,945 In this case, it is a JSONException This 49 00:03:42,945 --> 00:03:47,520 moves the responsibility of exception handling from this line here in 50 00:03:47,520 --> 00:03:51,330 getCurrentDetails to up here where the method is called. 51 00:03:52,590 --> 00:03:56,380 We see now in onResponse where getCurrentDetails is called, 52 00:03:56,380 --> 00:03:56,940 it has an error. 53 00:03:57,960 --> 00:04:01,900 Since we're already inside a try block, we can catch the JSONException down here. 54 00:04:03,650 --> 00:04:05,420 We'll catch the JSONException. 55 00:04:13,056 --> 00:04:15,120 And we'll add a log statement for it. 56 00:04:23,445 --> 00:04:28,029 Now our code is a bit more organized since our exceptions are all being handled in 57 00:04:28,029 --> 00:04:29,240 one place. 58 00:04:29,240 --> 00:04:33,720 With that exception handled, we can continue to set our JSON object. 59 00:04:33,720 --> 00:04:36,890 Let's take a quick look at the Android documentation on JSON object. 60 00:04:42,567 --> 00:04:44,470 Let's see what types of data we can get. 61 00:04:46,991 --> 00:04:50,770 We see that this class has several methods that allow to get different types of data. 62 00:04:57,537 --> 00:05:01,205 Strings, ints, booleans, JSON arrays. 63 00:05:01,205 --> 00:05:03,478 Even other JSON objects. 64 00:05:03,478 --> 00:05:07,120 We'll see some of these methods used in other projects, as well. 65 00:05:07,120 --> 00:05:09,310 Let's take another look at our data as a guide for 66 00:05:09,310 --> 00:05:11,893 how we should parse our JSON object. 67 00:05:11,893 --> 00:05:16,180 The JSON object forecast that we just created in our code 68 00:05:16,180 --> 00:05:19,130 corresponds to this root object up here. 69 00:05:21,770 --> 00:05:25,260 Remember our root object starts with a first opening brace. 70 00:05:25,260 --> 00:05:29,540 We're passing latitude and longitude in as parameters to the API call. 71 00:05:29,540 --> 00:05:31,700 So we don't need to worry about those. 72 00:05:31,700 --> 00:05:34,580 Let's use this timezone field as an example to start. 73 00:05:35,645 --> 00:05:39,820 timezone in all lower case is the key we want to get. 74 00:05:39,820 --> 00:05:42,940 The value is enclosed in double quotes. 75 00:05:42,940 --> 00:05:46,000 That tells us that it is a string value. 76 00:05:46,000 --> 00:05:48,220 Let's go back to our getCurrentDetails method. 77 00:05:52,325 --> 00:05:55,260 So we can access this timezone, and write it to the log. 78 00:05:57,360 --> 00:05:58,500 So String_ timezone. 79 00:06:02,049 --> 00:06:04,970 We're gonna get forecast.getString. 80 00:06:07,170 --> 00:06:09,470 We need to pass in the key we want. 81 00:06:09,470 --> 00:06:12,240 We say that it was timezone in all lower case. 82 00:06:16,773 --> 00:06:17,850 Then we want to log that. 83 00:06:20,580 --> 00:06:26,535 So Log.i, TAG say From JSON 84 00:06:26,535 --> 00:06:30,760 plus our timezone. 85 00:06:32,930 --> 00:06:36,010 Cool, we should test this out just to make sure we are, 86 00:06:36,010 --> 00:06:38,300 in fact, getting what we're expecting. 87 00:06:38,300 --> 00:06:42,450 This is important, especially when trying to parse through complex data. 88 00:06:42,450 --> 00:06:44,430 Building things one step at a time, and 89 00:06:44,430 --> 00:06:48,750 verifying them along the way is a great habit to get into. 90 00:06:48,750 --> 00:06:52,430 Otherwise, you can end up with a lot of code that can be broken in multiple 91 00:06:52,430 --> 00:06:53,700 places. 92 00:06:53,700 --> 00:06:55,280 Now, before we run it, 93 00:06:55,280 --> 00:06:58,590 we have another area here because we aren't returning anything. 94 00:06:58,590 --> 00:07:00,070 Let's just return null for now. 95 00:07:06,025 --> 00:07:06,890 Run our app. 96 00:07:12,776 --> 00:07:15,140 And checking here in the Logcat. 97 00:07:17,180 --> 00:07:20,790 Here's our parsed data from JSON, America/Los_Angeles. 98 00:07:20,790 --> 00:07:21,520 It's working! 99 00:07:21,520 --> 00:07:22,700 That's great! 100 00:07:22,700 --> 00:07:23,900 Let's take a quick break, and 101 00:07:23,900 --> 00:07:26,648 we'll get our current weather all set up when we come back.