1 00:00:00,630 --> 00:00:03,140 Our app is coming along nicely. 2 00:00:03,140 --> 00:00:07,100 We are able to get data from the network and account for situations where there is 3 00:00:07,100 --> 00:00:10,990 an error by giving the user an alert dialog to let them know. 4 00:00:10,990 --> 00:00:14,650 This makes for a much nicer user experience thus far. 5 00:00:14,650 --> 00:00:18,920 But what happens if we are trying to use the network when it isn't available? 6 00:00:20,080 --> 00:00:24,400 No data will be displayed, of course, which isn't a great user experience. 7 00:00:25,490 --> 00:00:29,050 However, a different exception is thrown that we can handle and 8 00:00:29,050 --> 00:00:32,100 turn it into a positive user experience. 9 00:00:32,100 --> 00:00:36,380 We should check to see if the network is available before we make the network call. 10 00:00:36,380 --> 00:00:38,880 If it is, we can proceed. 11 00:00:38,880 --> 00:00:44,210 If not, let's provide a message letting them know the network isn't available. 12 00:00:44,210 --> 00:00:48,220 Looking inside the onCreate method, we see that we are creating and 13 00:00:48,220 --> 00:00:52,260 executing a background thread to do our network operations. 14 00:00:52,260 --> 00:00:53,340 We can add our check for 15 00:00:53,340 --> 00:00:57,260 network availability inside there with an if statement. 16 00:00:57,260 --> 00:01:00,380 Let's create a new method to handle our network check as well. 17 00:01:00,380 --> 00:01:03,200 And use it in a condition for our if statement. 18 00:01:03,200 --> 00:01:09,500 Here, after we've defined our forecastURL, let's add an if statement, 19 00:01:09,500 --> 00:01:13,018 and then we can leverage some of Android Studio's quick fix help to complete it. 20 00:01:13,018 --> 00:01:21,052 So if, isNetworkAvailable, 21 00:01:22,129 --> 00:01:27,067 And then our closing parenthesis will be right down here, 22 00:01:27,067 --> 00:01:33,210 above our Log statement, and everything gets tabbed in, nice. 23 00:01:33,210 --> 00:01:40,400 If we click on isNetworkAvailable and hit Alt+Enter, we see the quick fix menu. 24 00:01:40,400 --> 00:01:43,379 Let's click on the Create isNetworkAvailable method option. 25 00:01:46,025 --> 00:01:51,130 Nice, Android Studio creates a private method for us, which is great. 26 00:01:51,130 --> 00:01:54,836 It has given it a return type of boolean, which is exactly what we want. 27 00:01:54,836 --> 00:01:58,470 Remember that we are using this inside a conditional check, so 28 00:01:58,470 --> 00:02:01,860 we'll want it to evaluate to true or false. 29 00:02:01,860 --> 00:02:04,690 Inside our method we'll be setting the return value 30 00:02:04,690 --> 00:02:06,770 based on the network availability. 31 00:02:06,770 --> 00:02:10,135 There is an Android class called ConnectivityManager that we can use. 32 00:02:13,385 --> 00:02:14,467 So ConnectivityManager, 33 00:02:19,824 --> 00:02:23,934 We'll call it manager, and I getSystemService. 34 00:02:27,291 --> 00:02:30,441 The parameter we need to pass into to getSystemService is the name of 35 00:02:30,441 --> 00:02:32,660 the service in string format. 36 00:02:32,660 --> 00:02:35,638 We can use a constant string from the context class here. 37 00:02:35,638 --> 00:02:42,480 Context.CONNECTIVITY_SERVICE. 38 00:02:43,870 --> 00:02:47,570 We see here that there is an error about incompatible types. 39 00:02:47,570 --> 00:02:51,500 The getSystemService method is returning a generic object. 40 00:02:51,500 --> 00:02:54,560 So we need to cast it to CONNECTIVITY_MANAGER. 41 00:02:54,560 --> 00:02:57,692 Fortunately, there's a quick fix for this too. 42 00:02:57,692 --> 00:03:01,280 Hit Alt+Enter, and 43 00:03:01,280 --> 00:03:05,220 Android Studio will take care of the casting for us, very cool. 44 00:03:06,230 --> 00:03:09,240 Next we need to instantiate a NetworkInfo object. 45 00:03:10,960 --> 00:03:14,024 Then on the next line NetworkInfo, 46 00:03:17,112 --> 00:03:20,988 networkInfo Alt+Enter for our imports. 47 00:03:24,436 --> 00:03:28,716 _manager.getActiveNetworkInfo. 48 00:03:31,694 --> 00:03:36,330 And it looks like there's a permissions error here for ACCESS_NETWORK_STATE. 49 00:03:36,330 --> 00:03:38,563 We should probably look in the documentation for 50 00:03:38,563 --> 00:03:41,399 this getActiveNetworkInfo method and see what it has to say. 51 00:03:54,974 --> 00:03:58,128 It's always a good idea to check the documentation about methods we are using 52 00:03:58,128 --> 00:03:59,570 for the first time. 53 00:03:59,570 --> 00:04:01,300 We see here in the Android docs, 54 00:04:01,300 --> 00:04:06,170 we need to add this ACCESS_NETWORK_STATE to our Android manifest file. 55 00:04:06,170 --> 00:04:07,952 Let's head over there and add that permission in. 56 00:04:10,919 --> 00:04:13,861 Go to AndroidManifest, 57 00:04:15,689 --> 00:04:19,114 Uses permission, and we want 58 00:04:19,114 --> 00:04:27,200 android.permission.ACCESS_NETWORK_STATE And close our tag. 59 00:04:28,880 --> 00:04:32,390 If we weren't aware of this permission issue, we probably would've 60 00:04:32,390 --> 00:04:37,570 written our code, ran our app, and seen an error that a permission was missing. 61 00:04:37,570 --> 00:04:41,860 Fortunately, permission error messages are typically pretty helpful. 62 00:04:41,860 --> 00:04:45,100 Remember back to the Internet permission error we saw earlier. 63 00:04:45,100 --> 00:04:46,945 This is where Logcat is great. 64 00:04:46,945 --> 00:04:52,360 Okay, we can now go back to MainActivity and our isNetworkAvailable method. 65 00:04:52,360 --> 00:04:54,573 Let's make a new variable called isAvailable, and 66 00:04:54,573 --> 00:04:55,936 we'll make it of Boolean type. 67 00:05:03,628 --> 00:05:06,346 In keeping with best practices when declaring variables, 68 00:05:06,346 --> 00:05:07,550 let's set this to false. 69 00:05:11,236 --> 00:05:17,318 Great, now inside this method we'll check to see if the network is available and 70 00:05:17,318 --> 00:05:19,840 if so, set isAvailable to true. 71 00:05:19,840 --> 00:05:21,860 We can use an if statement here for that check. 72 00:05:23,212 --> 00:05:28,130 So if, now what do we want to check here exactly? 73 00:05:28,130 --> 00:05:32,210 We want to check if there is a network and if it's connected to the internet. 74 00:05:32,210 --> 00:05:36,933 We can check multiple conditions on network info using the and operator. 75 00:05:36,933 --> 00:05:43,538 So if (networkInfo) != null && 76 00:05:43,538 --> 00:05:49,082 networkInfo.isConnected. 77 00:05:54,520 --> 00:05:56,452 In that case, isAvailable will be true. 78 00:05:59,005 --> 00:06:01,069 So if both of those conditions are true, 79 00:06:01,069 --> 00:06:03,750 we can safely set our isAvailable variable to true. 80 00:06:04,840 --> 00:06:08,080 Outside our if statement then we can return isAvailable. 81 00:06:12,577 --> 00:06:16,154 If we now look at our onCreate method, we can make sense of our if statement for 82 00:06:16,154 --> 00:06:17,980 the network check. 83 00:06:17,980 --> 00:06:22,110 When the condition is checked and isNetworkAvailable returns true, 84 00:06:22,110 --> 00:06:28,140 meaning the network and internet is available, our HTTP request will run. 85 00:06:28,140 --> 00:06:31,040 What if the network isn't available though? 86 00:06:31,040 --> 00:06:35,470 Out HTTP request won't run, and our application won't crash. 87 00:06:35,470 --> 00:06:37,760 But the user doesn't know that yet. 88 00:06:37,760 --> 00:06:40,840 We should let the user know that the network isn't available. 89 00:06:40,840 --> 00:06:43,400 Perhaps they have airplane mode turned on or 90 00:06:43,400 --> 00:06:45,200 need to do something else to get back online. 91 00:06:45,200 --> 00:06:48,330 It would be great to let them know there is an issue. 92 00:06:48,330 --> 00:06:51,570 Let's just add a simple toast message for the user. 93 00:06:51,570 --> 00:06:52,070 Go back down here. 94 00:06:57,290 --> 00:06:58,868 So else we'll do a Toast. 95 00:07:04,940 --> 00:07:08,829 We'll just tell them Sorry, the network is unavailable. 96 00:07:17,930 --> 00:07:20,944 LENGTH_LONG, And show. 97 00:07:24,948 --> 00:07:26,430 That line's a little long, so let's bring that down. 98 00:07:27,680 --> 00:07:30,540 While we're here, let's extract this string to our string resource and 99 00:07:30,540 --> 00:07:32,326 call it network_unavailable_message. 100 00:07:32,326 --> 00:07:37,319 Extract string resource, 101 00:07:37,319 --> 00:07:44,052 network_unavailable_message. 102 00:07:46,778 --> 00:07:50,130 And again, we can get that pop-up by hitting Alt+Enter on the string. 103 00:07:51,760 --> 00:07:56,400 Okay, for an extra challenge, see if you can make this message into a dialog 104 00:07:56,400 --> 00:07:58,360 fragment, just like our other error message. 105 00:07:59,530 --> 00:08:04,390 Great, we're checking for network availability and handling the errors. 106 00:08:04,390 --> 00:08:07,480 Let's test it to make sure all is working as planned. 107 00:08:07,480 --> 00:08:10,545 When we run our app, we should see our data in Logcat. 108 00:08:16,170 --> 00:08:16,670 There's out data. 109 00:08:18,929 --> 00:08:25,489 If we look at our emulator, Enter on Airplane Mode. 110 00:08:28,288 --> 00:08:30,695 Stop and restart Stormy. 111 00:08:39,220 --> 00:08:45,161 And there's 112 00:08:45,161 --> 00:08:50,564 our toast. 113 00:08:52,446 --> 00:08:54,730 Our app is off to a great start. 114 00:08:54,730 --> 00:08:58,860 We are successfully handling errors and getting weather data from the internet. 115 00:08:58,860 --> 00:09:01,230 Now, not to rain on our success, but 116 00:09:01,230 --> 00:09:05,370 there's a bit more work to be done to actually do something with our data. 117 00:09:05,370 --> 00:09:07,530 Let's take a look at that in the next stage.