Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Interacting with Retrofit4:47 with Jamie Huson
Now that we understand how our API works, we will use Retrofit to interact with it.
Retrofit: enables us to create readable Java interfaces and classes that match the API we are accessing, as well as allowing us to interact with simple Java objects instead of having to parse network responses manually
Bring in Retrofit to your API
Now that we know how the API works, let's use Retrofit to interact with it. 0:00 Retrofit uses interfaces to represent the API we are connecting to. 0:05 Let's start by creating a new Interface. 0:09 Here in my API package, I'm just going to create a new Java Class and 0:11 the type is going to be Interface. 0:16 I'm just gonna call it Api. 0:19 Next, we're going to add a method for each API call. 0:21 The API we're using returns back active listings from Etsy, so 0:24 I'm gonna call mine activeListings. 0:28 Now, Retrofit can be a little overwhelming when you're first learning how to use it, 0:32 but it has an elegant syntax that helps us understand what it's doing. 0:36 And, it does a lot of work for us, with only a little bit of code. 0:40 Retro fit uses annotations to tell it where the URI is and 0:44 what HTTP protocol to use. 0:48 From the documentation earlier, we know our protocol is get and 0:51 the URI is slash listing slash active. 0:55 Let's go ahead and annotate our method using the available retro fit annotations. 0:58 It's gonna ask us to import those as we would any other class or object type. 1:03 And, we also need to include a string that represents the URI, 1:08 which is /listings/active. 1:12 To include URL parameters, 1:14 we can include those as optional parameters in the active listings method. 1:18 I'm gonna annotate mine as Query, and I'm gonna give it a name. 1:23 The name is includes. 1:27 So, the includes was the image and 1:29 shop data that was coming through the API with our listings call. 1:31 We need to make sure we import those as well. 1:35 And, we need to give this a name as a variable. 1:40 Just gonna call it includes. 1:44 Finally, we need to include a callback. 1:46 Notice that the type of this method is void. 1:48 That means it's going to callback immediately once you call it. 1:50 But, we need to include a callback which when the response happens and 1:53 completes, we'll get a callback on the main UI thread. 1:57 Retrofit includes a class called Callback. 2:00 The callback mechanism is just like what we used in the weather app using OkHttp in 2:03 an earlier course. 2:07 This callback is gonna be typed to the Java class that the network response will 2:09 be converted into by Retrofit. 2:13 This means that Retrofit automatically parses the network response, and 2:15 creates an object of the type you want, with all the data set on it. 2:18 All you have to do is give it the class that corresponds to 2:22 the response from the API. 2:25 In this case, we wanna go ahead and use the ActiveListings class. 2:27 Again, we wanna go ahead and import this. 2:31 Great. 2:34 Our API is all set up now. 2:35 Next, we're going to add to our Etsy class. 2:37 We're gonna add a method to retrieve back an instance of the API interface we 2:40 just built. 2:43 We're gonna open the Etsy class, let's go ahead and add a method in here. 2:45 Private, static. 2:49 And, it's gonna return back the API we just built. 2:50 And, I'm gonna call it getApi. 2:54 We're going to use the included RestAdapter class 2:55 that's a part of Retrofit. 2:59 RestAdapter has a builder that's a part of it, 3:01 so we're gonna say new RestAdapter.Builder, and 3:04 with the builder pattern we can easily configure it with any options we need. 3:07 The first we're gonna do is set the end point. 3:11 So, from the Etsy documentation, 3:14 we know the endpoint is https://openapi.etsy.com/v2. 3:17 Once we set the end point, we can just call .build and then .create. 3:23 And, when you call .create what you're gonna do 3:30 is pass in the interface that you wanted to create an object from. 3:33 So we're gonna pass in the Api class. 3:37 Now, this going to return back an instance of Api with the end point set to it. 3:40 One thing we might've noticed is that we haven't set up the API 3:46 key anywhere in the request. 3:49 We can do this by adding in a request interceptor which allows us to 3:50 append a parameter at the end of the request after it's initially created, so 3:55 let's go ahead and make a method that will do this. 3:59 I'm going to create another method and 4:01 it's going to return back a request interceptor. 4:03 Just gonna call it getInterceptor. 4:07 And, it's gonna return back a new request interceptor. 4:13 What I'm gonna do is just add in the parameter that I need, 4:17 my API key with every request. 4:20 So, I'm gonna take the request and just say addEncodedQueryParameter. 4:23 And, the name of it is api key. 4:28 And, we started here in this value of capital API KEY. 4:31 We just append it there. 4:35 Finally, back in our builder, 4:38 we can call into setRequestInterceptor and call getInterceptor. 4:40
You need to sign up for Treehouse in order to download course files.Sign up