1 00:00:00,540 --> 00:00:03,790 Let us start by talking a bit about the tricatch block we have in our code. 2 00:00:05,160 --> 00:00:09,390 The idea here is that if something bad happens in our app, the Java runtime 3 00:00:09,390 --> 00:00:13,730 system will kind of raise a little flag that says something is wrong. 4 00:00:13,730 --> 00:00:17,810 If we are on the lookout for these flags, we can handle them appropriately and 5 00:00:17,810 --> 00:00:21,110 save our users from a crash or bad experience. 6 00:00:21,110 --> 00:00:25,674 The act of raising that little flag is called throwing an exception 7 00:00:25,674 --> 00:00:28,534 when we must catch them in code in our app. 8 00:00:28,534 --> 00:00:31,696 A lot of methods we use throw exceptions like these. 9 00:00:31,696 --> 00:00:35,710 The response.issuccesful method is one example. 10 00:00:35,710 --> 00:00:39,940 It throws a specific type of exception called an IO exception. 11 00:00:39,940 --> 00:00:43,120 Which stands for input output exception. 12 00:00:43,120 --> 00:00:46,330 Specific exceptions like this are typically 13 00:00:46,330 --> 00:00:49,830 subclasses of a base class called exception. 14 00:00:49,830 --> 00:00:51,140 What can we do about them? 15 00:00:51,140 --> 00:00:54,760 We need to wrap our code in something called a tri catch block, 16 00:00:54,760 --> 00:00:55,490 like we have here. 17 00:00:59,190 --> 00:01:04,350 Our code then is essentially saying, try to check if the response is successful. 18 00:01:04,350 --> 00:01:06,310 If so, do something. 19 00:01:06,310 --> 00:01:08,840 In this case, output to the log the forecast data. 20 00:01:09,850 --> 00:01:15,010 If it fails with an IOException, run this other code to handle the exception. 21 00:01:15,010 --> 00:01:16,960 Otherwise, we just proceed as normal. 22 00:01:17,960 --> 00:01:21,850 In this specific case, we are just logging the exception. 23 00:01:21,850 --> 00:01:23,950 But we could take other appropriate action as well. 24 00:01:25,260 --> 00:01:29,820 Next, let's talk about how to handle the case where the response is not successful. 25 00:01:29,820 --> 00:01:32,355 We want to add an else block here. 26 00:01:32,355 --> 00:01:37,350 Down under the if, I like to drop this down. 27 00:01:39,200 --> 00:01:40,710 That's just a matter of personal taste. 28 00:01:42,030 --> 00:01:46,254 So inside here let's Log.v(TAG). 29 00:01:46,254 --> 00:01:49,318 It would probably be helpful to know what the response looks like. 30 00:01:52,802 --> 00:01:57,660 Response.body().string. 31 00:01:57,660 --> 00:02:02,470 Now to make our code follow the dry principle of, don't repeat yourself. 32 00:02:02,470 --> 00:02:05,469 Let's move the log statement up here to the top of the try statement. 33 00:02:18,839 --> 00:02:20,222 We can get rid of that one. 34 00:02:22,764 --> 00:02:26,582 Now it would be great to alert the user about the error somehow. 35 00:02:26,582 --> 00:02:30,100 After all, they won't see the log messages in the app. 36 00:02:30,100 --> 00:02:34,230 Let's create a new method inside our else statement to do just that. 37 00:02:34,230 --> 00:02:38,297 Call it alertUserAboutError. 38 00:02:41,982 --> 00:02:43,096 You can come over here now. 39 00:02:43,096 --> 00:02:48,140 Hit Alt + Enter. 40 00:02:48,140 --> 00:02:52,161 For the quick fix pop-up, we want to create a new method or 41 00:02:52,161 --> 00:02:53,975 alertuUserAboutError. 42 00:02:53,975 --> 00:02:56,141 And we wanna create it here in MainActivity, 43 00:02:56,141 --> 00:02:57,856 not in this anonymous inner class. 44 00:02:57,856 --> 00:02:59,117 We want the main activity. 45 00:03:01,277 --> 00:03:02,520 Scroll up a little bit. 46 00:03:03,820 --> 00:03:05,220 Inside our new method, 47 00:03:05,220 --> 00:03:09,470 we want to do something to visually alert the user that an error has occurred. 48 00:03:09,470 --> 00:03:12,450 We could certainly use a toast as we've seen before. 49 00:03:12,450 --> 00:03:16,390 But the issue with toast method is that they only stay on the screen for 50 00:03:16,390 --> 00:03:17,150 a short period of time. 51 00:03:17,150 --> 00:03:20,590 If we want to make sure the user sees a message, 52 00:03:20,590 --> 00:03:24,060 we should use something that requires them to confirm or dismiss it. 53 00:03:24,060 --> 00:03:27,630 Android provides a nice way to do this with dialogues. 54 00:03:27,630 --> 00:03:31,839 Let's go to the Android docs and check out the documentation for dialogues. 55 00:03:40,304 --> 00:03:45,140 Dialogs pop up over the activity and ask the user to do something. 56 00:03:45,140 --> 00:03:47,850 You're probably familiar with them in other apps, and 57 00:03:47,850 --> 00:03:49,780 this is a great use for them. 58 00:03:49,780 --> 00:03:52,450 They can be simple or quite complex. 59 00:03:52,450 --> 00:03:55,910 We'll just build a simple one for our StoreMe app that has an error message and 60 00:03:55,910 --> 00:03:56,870 an OK button. 61 00:03:57,900 --> 00:03:59,819 We scroll down on this page a little bit 62 00:04:02,009 --> 00:04:06,920 We read that we should use a dialog fragment as a container for our dialogs. 63 00:04:06,920 --> 00:04:09,280 Now, it is kind of a lot of code for 64 00:04:09,280 --> 00:04:12,180 what appears in the app as a pretty simple thing. 65 00:04:12,180 --> 00:04:15,528 The good news though is that once you have this example working, 66 00:04:15,528 --> 00:04:19,350 you'll have code you can easily reuse and adapt in the future. 67 00:04:19,350 --> 00:04:23,250 Let's head back to Android Studio into our code and add a dialog to our app. 68 00:04:24,440 --> 00:04:25,720 So first, we'll need a new class for 69 00:04:25,720 --> 00:04:28,120 that dialog fragment, as mentioned in the documentation. 70 00:04:29,590 --> 00:04:31,058 We can do that here in the Project pane. 71 00:04:31,058 --> 00:04:35,886 Select Java, To New Java Class. 72 00:04:38,674 --> 00:04:43,873 We'll call it AlertDialogFragment. 73 00:04:47,780 --> 00:04:52,442 And for the super class, we will want to 74 00:04:52,442 --> 00:04:57,410 put in android.app.DialogFragment. 75 00:04:57,410 --> 00:05:02,241 Everything else is okay Fragments 76 00:05:02,241 --> 00:05:06,320 are similar to activities and are very useful in certain situations. 77 00:05:06,320 --> 00:05:09,840 We'll cover fragments in more depth in future projects. 78 00:05:09,840 --> 00:05:13,050 For now, we need to override one key method from dialog fragment. 79 00:05:14,160 --> 00:05:18,154 Type onCreateDialog and hit Enter for auto-complete. 80 00:05:19,910 --> 00:05:22,670 The method will be called when we create the dialog. 81 00:05:22,670 --> 00:05:26,310 We will do this from our activity in a moment. 82 00:05:26,310 --> 00:05:28,610 We need to add code here to configure the dialog. 83 00:05:29,700 --> 00:05:34,810 To do that, we will use a special dialog class called AlertDialog. 84 00:05:34,810 --> 00:05:40,478 So we can delete this line here, And replace it with the following. 85 00:05:40,478 --> 00:05:44,112 AlertDialog.Builder. 86 00:05:46,012 --> 00:05:49,750 Builder, option enter for inputs. 87 00:05:49,750 --> 00:05:52,270 And we want that first one there. 88 00:05:52,270 --> 00:05:52,970 Not the support. 89 00:05:54,090 --> 00:05:57,470 We want a new AlertDialog.Builder. 90 00:05:57,470 --> 00:05:59,820 Now we need to pass in the context. 91 00:05:59,820 --> 00:06:02,315 Normally we would use this or 92 00:06:02,315 --> 00:06:08,400 activity.this to refer to the current context which has been the activity. 93 00:06:08,400 --> 00:06:13,830 However, now we're in a different class so what do we use for our context? 94 00:06:13,830 --> 00:06:17,680 Fortunately, the DialogFragment class that we're extending 95 00:06:17,680 --> 00:06:23,150 has a useful method to get the activity where this DialogFragment was created. 96 00:06:23,150 --> 00:06:26,550 We call it with getActivity which will? 97 00:06:26,550 --> 00:06:31,060 Yeah, you guessed it, give us the activity and the context we need. 98 00:06:31,060 --> 00:06:33,152 So we can put in, getActivity. 99 00:06:35,306 --> 00:06:39,324 Let's actually make it into a variable as well, that will be useful shortly. 100 00:06:43,368 --> 00:06:50,660 So context context, do our imports, Get activity. 101 00:06:50,660 --> 00:06:53,464 And then we can pass in contacts down here. 102 00:06:58,378 --> 00:07:02,130 Notice that our data type is alertDialog.builder. 103 00:07:02,130 --> 00:07:05,450 Builder is a nested class inside the alert dialogue class. 104 00:07:06,580 --> 00:07:08,940 As a quick side note here, the reasons for 105 00:07:08,940 --> 00:07:13,760 this have to do with a software design pattern called the factory method pattern. 106 00:07:13,760 --> 00:07:16,900 I've put links in the teacher's notes for further reading on this. 107 00:07:16,900 --> 00:07:18,600 Now is a great time for a quick break. 108 00:07:18,600 --> 00:07:20,910 When we get back, we'll setup this alert dialogue.