Introducing Retrofit11:26 with Jamie Huson
Retrofit makes performing Http calls easy by having a concise easy-to-read syntax, straight-forward integration to a project, and supporting the Android threading model as a default.
[MUSIC] 0:00 Hi, my name is Jamie Huson and I'm an Android Developer at Etsy. 0:04 In this workshop, we're going to integrate the Retrofit library from Square into 0:09 an app to show how easy working with an API can be. 0:13 Retrofit makes performing HTTP calls easy by having a concise, 0:17 easy to read syntax, straightforward integration to a project, and support for 0:21 the Android Freddie model as a default. 0:26 Let's break down how it does this. 0:30 Retrofit consists of four parts. 0:32 An interface that declares the end points, 0:34 their HTTP types, callback types, and parameters. 0:37 The methods are annotated making it easy to configure an endpoint without 0:41 much code at all. 0:45 Here's an example of a basic HTTP get call. 0:47 Next, is the adapter. 0:50 The adapter transforms your interface into an object instance you can work with. 0:52 An adapter can be configured with additional information about your 0:58 HTTP stack. 1:01 A converter will be used to convert the returned binary data 1:02 into a java object you can easily use. 1:06 Finally, a call and callback mechanism, to have fine grained control over when and 1:09 how requests are executed. 1:14 In this app, we're going to connect to the image or API, we'll be able to anonymously 1:18 upload images or sign in for a user and upload images to their account. 1:22 I've supplied starter project files for you, you can import these to 1:27 Android Studio or create your own new project and drop the files in yourself. 1:31 Here I've got the start files open in a project. 1:36 So let's check out what I gave you. 1:39 First thing we're gonna look at is our build.gradle file for our app, 1:42 let's go ahead and open that. 1:45 And what you'll see here is that near the bottom, I've added some dependencies for 1:48 us already and that includes the most important one, retrofit, 1:52 what we're gonna be learning today. 1:56 And also the second part of that is the retrofit converter using gson. 1:58 So it's important that your code matches exactly what I'm doing here, so 2:04 make sure you use the same version as me. 2:07 You can upgrade to a more recent version but 2:10 there may be changes in how the code is written. 2:13 You can also see if supplied the main activity, 2:15 base application an adapter for a list. 2:18 This classical o op util, we'll go back to that later, and some model classes here. 2:22 Next we need to look at the documentation for 2:28 the API we're connecting to, in this case imager. 2:30 Let's go ahead and go to the browser and go to their documentation site for 2:34 their API, it's api.imgur.com. 2:37 And if you don't have an account with a Imgur, go ahead and create one. 2:41 And then, what we're going to do is we're going to go up to our account, 2:44 in the upper right, and go to settings. 2:47 What we're going to look at is applications. 2:50 So I've created an application here if you haven't created one, go ahead and 2:54 create one now. 2:58 And what you'll see is that it has a name of called mine Retrofit Test, 2:58 you can call it whatever you want and a client ID, that's really important we're 3:03 gonna need that we don't need the secret but we do need this thing called redirect. 3:07 So you can see here if I click edit what mine is, 3:12 it's https://treehouseworkshop:88. 3:15 :88 is a port, so enter yours something like this. 3:19 If you're not sure, just put exactly what you see here on the screen. 3:25 Okay, so once we have all those applications set up with the API 3:31 what we're going to do is we need to authorize our app to work for 3:35 users, that will allow us to upload images to their account. 3:39 So to do this what we're going to do is use something called oauth and 3:43 this has a flow to it. 3:47 And it's described here in the documentation in detail under 3:48 authentication and oauth 2.0 you can read more about that but 3:54 the just of it is this we're gonna direct the user to a URL in the browser and 3:58 in that URL we're going to put our client ID. 4:03 Then the user is going to sign in and the browser will redirect to another URL, 4:07 that's our redirect URL we just supplied in the application settings. 4:12 So our apps can intercept that URL and direct users back into our out. 4:16 And then we're gonna parse out 4:20 some really important information on how to connect to image or API. 4:23 The most important one is the access token that it provides for us. 4:27 So to begin, let's go ahead and create a place in our code base to store all these 4:32 constants that we need to know about for connecting to this API, 4:36 all this information, is found here in the API documentation, and so 4:40 if you have any questions you can come back, read through this, 4:44 and look it up they're switching to the code base. 4:47 Okay so the first thing we wanna do is we wanna create a place to store what URL 4:55 we're gonna be connecting to. 4:59 So in our API let's go ahead and create a new Java class let me call it 5:02 Imager and it's actually not going to be just a class I want to be an interface. 5:09 And in here, we're going to go and 5:17 store the basic information about connecting to images API.. 5:18 It's pretty string we'll call it an imgur or base URL, 5:22 and this is the base your ID, connect to their API. 5:28 Okay, the next thing we need to do is we need to store that client ID, 5:37 we're going to use that to connect. 5:41 So you wanna go ahead and go to the web site again, go 5:45 to your account settings, go to your applications, and copy in that ID. 5:51 And then we're going 5:57 to create this URL that 6:02 has our client ID in it, and this is what that URL looks like. 6:07 Again, it's documented in the documentation, 6:13 you can read about it there. 6:15 This is what it looks like, I'm gonna call it AUTHORIZATION_URL. 6:17 And it looks like this. 6:21 API, and then oauth2, authorize and 6:23 one of the parameters we need 6:30 to include is the client ID. 6:35 So, it's client_id and then I'm just going to append client ID to it. 6:40 And then this ends with a final type, 6:47 which is what we want to get back from this. 6:52 What we want to get back is a token, so let's go ahead and tell it that. 6:54 Response type is the parameter, we just want to say, we want a token back. 7:03 Okay, finally let's go ahead and store that redirect URL. 7:09 Next, we need to add in something to our manifest and this is what's gonna allow 7:27 us to redirect the user back to our app once they've signed in and 7:32 gone through the authorization process on the imagur website. 7:36 It's going to open up our manifest and 7:40 scroll down, and you can see this block is really important. 7:44 I've already put it in there for you but 7:51 it's important that you know this is in there. 7:52 So what we're saying is that if we're going to VIEW this URL, starts with https, 7:55 treehouseworkshop, the port is 88, then we want to go back into our app. 8:02 So if you don't have this in there this isn't going to work so 8:07 make sure that this is included in your project. 8:11 If you use my starter files it's already in there. 8:13 Okay, now let's place all the information we get back from the API 8:18 into a shared preference object. 8:23 So the API is gonna give us back this token, and we want to be able to store 8:25 that so that we can make requests every time the user starts the app. 8:29 So let's go out and put in a shared preference. 8:33 And I've started that already using this oauth2 class, so let's open that up. 8:35 We can see here I have added some todos for you. 8:40 Need to put the constants in the top, let's go ahead and 8:44 start doing that right here. 8:47 So again these represent all the data we're going to get back 8:51 from the imager API, we're gonna get back an access token. 8:55 Gonna get back a value called expires_in, 9:04 this tells us how many milliseconds our access token is valid for 9:08 before it expires and we need to authorize the app again. 9:13 We're gonna get the type or the token type specifically. 9:23 And we're gonna get something called the refresh token. 9:33 This is what you use once your token has expired to get a new token. 9:38 And finally something that's going to get 9:47 passed to us is the username of the account that we're accessing. 9:50 Okay, so what you'll see is that I'm keeping a singleton 10:01 of the shared preference in this class. 10:04 And I wrote a little TODO here, in case you're not using my starter files. 10:07 In your base application, make sure you call init here, 10:11 this creates a new instance. 10:14 So there are some getters and setters for storing data into the shared preference. 10:21 And the last thing we need to do here is we need to complete the logic for 10:26 this helper called isAuthorized. 10:30 So isAuthorized is going to tell us do we have a valid token or not, so 10:32 to compute whether we have a valid token, what we need to do is return. 10:37 If we have an access token at all, so that should be not null. 10:46 And we want to make sure that the expires in value is still valid, 10:53 so let's call getLong and get the EXPIRES_IN value. 10:59 And make sure that that's less than the current time on the system. 11:03 So if both these cases are true, then we have a valid token and we're authorized. 11:10 That's good for now, when you come back we'll authenticate our app for the user. 11:16 Now that we know about how Retrofit works, let's start writing our app. 11:22
You need to sign up for Treehouse in order to download course files.Sign up