This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
How do we handle the scenario where there isn't even a network connection available? (Oh, the horror!)
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Our app is coming along nicely.
0:00
We are able to get data from the network
and account for situations where there is
0:03
an error by giving the user
an alert dialog to let them know.
0:07
This makes for
a much nicer user experience thus far.
0:10
But what happens if we are trying to use
the network when it isn't available?
0:14
No data will be displayed, of course,
which isn't a great user experience.
0:20
However, a different exception
is thrown that we can handle and
0:25
turn it into a positive user experience.
0:29
We should check to see if the network is
available before we make the network call.
0:32
If it is, we can proceed.
0:36
If not, let's provide a message letting
them know the network isn't available.
0:38
Looking inside the onCreate method,
we see that we are creating and
0:44
executing a background thread
to do our network operations.
0:48
We can add our check for
0:52
network availability inside
there with an if statement.
0:53
Let's create a new method to
handle our network check as well.
0:57
And use it in a condition for
our if statement.
1:00
Here, after we've defined our forecastURL,
let's add an if statement,
1:03
and then we can leverage some of Android
Studio's quick fix help to complete it.
1:09
So if, isNetworkAvailable,
1:13
And then our closing parenthesis
will be right down here,
1:22
above our Log statement, and
everything gets tabbed in, nice.
1:27
If we click on isNetworkAvailable and
hit Alt+Enter, we see the quick fix menu.
1:33
Let's click on the Create
isNetworkAvailable method option.
1:40
Nice, Android Studio creates a private
method for us, which is great.
1:46
It has given it a return type of boolean,
which is exactly what we want.
1:51
Remember that we are using this
inside a conditional check, so
1:54
we'll want it to evaluate to true or
false.
1:58
Inside our method we'll be
setting the return value
2:01
based on the network availability.
2:04
There is an Android class called
ConnectivityManager that we can use.
2:06
So ConnectivityManager,
2:13
We'll call it manager,
and I getSystemService.
2:19
The parameter we need to pass into
to getSystemService is the name of
2:27
the service in string format.
2:30
We can use a constant string
from the context class here.
2:32
Context.CONNECTIVITY_SERVICE.
2:35
We see here that there is an error
about incompatible types.
2:43
The getSystemService method is
returning a generic object.
2:47
So we need to cast it to
CONNECTIVITY_MANAGER.
2:51
Fortunately, there's a quick fix for
this too.
2:54
Hit Alt+Enter, and
2:57
Android Studio will take care of
the casting for us, very cool.
3:01
Next we need to instantiate
a NetworkInfo object.
3:06
Then on the next line NetworkInfo,
3:10
networkInfo Alt+Enter for our imports.
3:17
manager.getActiveNetworkInfo().
3:24
And it looks like there's a permissions
error here for ACCESS_NETWORK_STATE.
3:31
We should probably look
in the documentation for
3:36
this getActiveNetworkInfo method and
see what it has to say.
3:38
It's always a good idea to check the
documentation about methods we are using
3:54
for the first time.
3:58
We see here in the Android docs,
3:59
we need to add this ACCESS_NETWORK_STATE
to our Android manifest file.
4:01
Let's head over there and
add that permission in.
4:06
Go to AndroidManifest,
4:10
uses-permission, and we want
4:15
android.permission.ACCESS_NETWORK_STATE
and close our tag.
4:19
If we weren't aware of this permission
issue, we probably would've
4:28
written our code, ran our app, and seen
an error that a permission was missing.
4:32
Fortunately, permission error messages
are typically pretty helpful.
4:37
Remember back to the Internet
permission error we saw earlier.
4:41
This is where Logcat is great.
4:45
Okay, we can now go back to MainActivity
and our isNetworkAvailable method.
4:46
Let's make a new variable
called isAvailable, and
4:52
we'll make it of Boolean type.
4:54
In keeping with best practices
when declaring variables,
5:03
let's set this to false.
5:06
Great, now inside this method we'll check
to see if the network is available and
5:11
if so, set isAvailable to true.
5:17
We can use an if statement here for
that check.
5:19
So if,
now what do we want to check here exactly?
5:23
We want to check if there is a network and
if it's connected to the internet.
5:28
We can check multiple conditions on
network info using the and operator.
5:32
So if (networkInfo) != null &&
5:36
networkInfo.isConnected.
5:43
In that case, isAvailable will be true.
5:54
So if both of those conditions are true,
5:59
we can safely set our
isAvailable variable to true.
6:01
Outside our if statement then
we can return isAvailable.
6:04
If we now look at our onCreate method,
we can make sense of our if statement for
6:12
the network check.
6:16
When the condition is checked and
isNetworkAvailable returns true,
6:17
meaning the network and internet is
available, our HTTP request will run.
6:22
What if the network
isn't available though?
6:28
Out HTTP request won't run, and
our application won't crash.
6:31
But the user doesn't know that yet.
6:35
We should let the user know that
the network isn't available.
6:37
Perhaps they have airplane
mode turned on or
6:40
need to do something
else to get back online.
6:43
It would be great to let
them know there is an issue.
6:45
Let's just add a simple toast message for
the user.
6:48
Go back down here.
6:51
So else we'll do a Toast.
6:57
We'll just tell them Sorry,
the network is unavailable.
7:04
LENGTH_LONG, and show.
7:17
That line's a little long,
so let's bring that down.
7:24
While we're here, let's extract this
string to our string resource and
7:27
call it network_unavailable_message.
7:30
Extract string resource,
7:32
network_unavailable_message.
7:37
And again, we can get that pop-up
by hitting Alt+Enter on the string.
7:46
Okay, for an extra challenge, see if
you can make this message into a dialog
7:51
fragment, just like our
other error message.
7:56
Great, we're checking for network
availability and handling the errors.
7:59
Let's test it to make sure
all is working as planned.
8:04
When we run our app,
we should see our data in Logcat.
8:07
There's our data.
8:16
If we look at our emulator,
Enter on Airplane Mode.
8:18
Stop and restart Stormy.
8:28
And there's
8:39
our toast.
8:45
Our app is off to a great start.
8:52
We are successfully handling errors and
getting weather data from the internet.
8:54
Now, not to rain on our success, but
8:58
there's a bit more work to be done to
actually do something with our data.
9:01
Let's take a look at
that in the next stage.
9:05
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up