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
We need to initialize an OkHttp client to request data using OkHttp. In this video, we'll see how to set up a request to get data from our custom Dark Sky URL.
Additional Information
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
We've been building up to this for
0:00
a while now, so let's, well, there's
something else I'd like to show you.
0:01
Don't get too mad, but this is going
to make you an even better developer.
0:06
Really, it will.
0:11
We're going to start of making by
making our request the wrong way.
0:12
Yes, that's right.
0:16
I'm going to intentionally show you
how to do something the wrong way.
0:18
But only to reinforce and
better explain the correct way.
0:22
The code we wind up writing
here will still be useful, so
0:26
make sure you follow along.
0:30
We'll clean it up shortly.
0:31
We actually haven't run our app yet since,
well, there really isn't much to run.
0:34
Let's make sure that we get our
emulator up and running, though, so
0:39
it will be ready for us here in a bit.
0:42
We'll start
the Android Virtual Device Manager,
0:44
AVD Manager,
select a device to emulate and run.
0:46
Great, we can set that into the background
now that it's up and running.
0:53
And we can close this out.
0:58
Now, we need to get some
info from Dark Sky.
1:01
Let's go back here to their developer
site and copy this sample API call URL.
1:03
This will let us get the weather for
1:10
Alcatraz Island in the San Francisco
bay of California.
1:12
Now, let's go back to Android Studio and
put it to work.
1:15
Let's go into main activity,
under app, and java,
1:18
under com.teamtreehouse.stormy
here in MainActivity.
1:23
So here, in the onCreate method,
let's build a URL.
1:27
We'll call it String forecastURL.
1:34
I'll set it equal to our string
we just copied from Dark Sky.
1:40
Seems like it would make sense
to refactor that a bit and
1:45
pull out some of
the information in that URL.
1:48
Things like our API key, latitude,
1:51
and longitude would be better
served to be their own variables.
1:53
Let's set that up.
1:57
So let's set up a string for API key.
2:00
And we can just cut this out or
copy and paste that.
2:07
And our longitude and latitude are double,
so we can add those in.
2:14
Latitude.
2:21
Copy our latitude here.
2:25
Longitude.
2:33
Grab our longitude value.
2:35
Now, we can reformat our
forecast URL variable.
2:41
Come in here,
2:46
concatenate our API key.
2:48
Slash, no spaces.
2:56
Latitude Put that comma in there.
3:00
Again, no spaces.
3:08
We want our longitude.
3:08
We can get rid of all that.
3:13
That line's a little bit long,
so let's drop that down.
3:15
Cool, now we need to use this.
3:19
Let's look back at OkHttp site.
3:22
So back here at their website
we have this wiki link.
3:25
From here, using OkHttp.
3:29
And then go to Recipes.
3:32
There's several of these recipes
available, so let's see here.
3:35
This Synchronous Get looks promising.
3:39
It says that it downloads a file, prints
the header, and the response body or
3:42
data as a string.
3:46
That sounds like what we need.
3:48
We know, because of the API
documentation in its contract with us,
3:50
that we'll get back JSON data.
3:54
We could do a lot of different
things with it, which we'll get to.
3:57
For now, however,
4:00
let's just print the data to
the log to get something happening.
4:01
We look down here, we see a recipe for
their Synchronous Get methods.
4:05
So let's work through this
recipe bit by bit here.
4:10
It looks a little daunting,
and with a lot of steps, but
4:12
they start with small pieces and
we'll build from there.
4:16
Recipes like this are great because
once they make sense you can copy and
4:19
paste them without having to
remember all the details each time.
4:23
So let's head back over to Android Studio,
and our onCreate method.
4:27
So first, we'll need to create
an OkHttp client object.
4:32
So down here, OkHttpClient.
4:38
We'll call it client.
4:43
It's a new OkHttpClient.
4:45
We'll use that as our main client object.
4:51
Next, we need to build a request for
the client to send to the Dark Sky server.
4:55
Recall from earlier that this is
part of the request response cycle.
5:00
Do request.
5:05
We'll call it request.
5:08
It's a new request builder.
5:11
And we can chain together the necessary
methods for a new request object.
5:15
So we want URL.
5:20
We're gonna pass it in the forecast URL.
5:22
And we'll ask it to build.
5:24
I find it a little bit more readable to
have these methods on separate lines.
5:28
So let's re-factor that just slightly.
5:32
And we'll put these on separate lines.
5:35
Next, we need to put this
request inside a call object.
5:40
We'll do call.
5:50
We'll call it call.
5:51
Client, new call.
5:55
We'll pass in a request.
5:56
Option enter to handle our imports.
5:59
Nice, the call class has an execute method
that we can use here on our call object
6:04
to get the response.
6:09
Let's store that in a response variable.
6:11
Response.
6:15
Call it response.
6:16
Option enter, for our imports.
6:18
call.execute().
6:21
Well, Android Studio is now
showing that we have an error.
6:25
We hover over it, we see that it's
an unhandled exception error.
6:29
This is okay and to be expected.
6:34
The execute method throws an exception and
we aren't handling, we're catching it.
6:37
You may already know this
from our JAVA courses.
6:42
If not, there's no reason to worry,
we'll discuss it shortly.
6:44
For now,
6:48
place your cursor somewhere in the air
designated by this red squiggly line.
6:49
Hit Alt+Enter,
which pops up the quick fix.
6:53
First option there is the one we want,
Surround with try/catch.
6:58
Great, however, let's update this
to print the error to the log.
7:04
Log error.
7:12
Exception caught.
7:18
Log error Get our imports.
7:23
Let's go back up here and define our tag.
7:31
Public static final String call it TAG.
7:38
MainActivity.class.getSimpleName().
7:45
Nice!
7:51
Now, let's go back and add a check to
see if the request was successful.
7:52
We can use a convenient method from
the response class to do this.
7:55
Right after our execute line, let's add
an IF statement to check the success.
7:59
So IF response is
8:05
successful.
8:10
We'll just log the entire response body.
8:20
TAG response.body().string().
8:22
Note here that the string method is
not to be confused with toString.
8:32
This comes from the example
recipe on the okHhttp site.
8:36
All right, we've set up our URL and
8:40
followed the okHttp recipe,
so we should be all set.
8:43
We should be able to see
the response printed to the log
8:47
as soon as our activity starts.
8:50
Let's test it out.
8:51
Run our app.
8:54
Select that device.
8:55
Go over here to the emulator.
8:57
There's an error.
9:00
Let's look here in the log and
see what it is.
9:02
Span this up a little bit.
9:08
Looking here in the log, we see it is
a network on main thread exception error.
9:11
It's the one I expected.
9:19
Recall I said I'd show
you the wrong way first.
9:22
This is it.
9:25
We were using a code for
asynchronous get request.
9:25
Okay, so we have an error.
9:30
It's really not the end of the world.
9:32
And in fact, it's a huge learning benefit.
9:33
Plus, we've done a lot
of things correctly.
9:37
We made a basic request
from a web to get data.
9:39
That's powerful in and of itself.
9:42
Before we dive in to fix that error,
9:45
I'd like to have a discussion
with you about concurrency.
9:47
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