Retrofit Tutorial — Getting Started and Creating an Android Client

preview_player
Показать описание
In this video, you'll learn what Retrofit is and how to request data from the GitHub API.

Tip: turn on subtitles to deal with my accent. A shortened transcript is also available below.

GitHub Repository for the code:

Watch 20+ Retrofit videos in our playlist here:

----------------------------------------

Our book on Retrofit is available on leanpub:

----------------------------------------

Checkout 320+ technical in-depth tutorials:

Subscribe for two new videos every week:

----------------------------------------

Follow us on social media to get updates on new content:

----------------------------------------

Shortened Transcript:

In this video, you'll learn two things. I'll explain what Retrofit is
and why you should use it. We'll go through an example with
all the necessary steps to interact with the GitHub API.
Specifically, we'll request all public GitHub repositories of a user and display them in our Android app.
All you have to do with Retrofit is to describe the endpoints; what they expect and what they respond and you're good to go!
This makes it so much easier to develop apps, which interact with APIs.
Alright, you need to add Retrofit as a Gradle dependency.
Retrofit uses OkHttp as a network layer and automatically pulls it in for us as a dependency.
Since GitHub uses JSON as a data format for responses and requests, you'll need a converter to convert our Java objects to and from JSON.
We want to have repositories from a user so let's call reposForUser.
We also want to pass a parameter for the user want to request the repositories for.
Finally, we've to describe what the response is and it is a list of GitHub repositories.
We'll need to annotate the method with more meta information.
First, we need to specify that this is a GET request and we do that by the @GET() annotation.
Then we'll also tell where the endpoint for the request is.
Usually, you want to use a relative and not the full URL.
We want to replace this user name with this dynamically.
Retrofit has something called "path parameters", which we'll talk more in detail in a later video.
Now we can decide during runtime, which user we want to request the repositories for.
If you want to make requests from a UI thread, we'll need to do this asynchronously. We need to wrap our return into a Call<> object.
So the return isn't a List<> it's a Call<List<>> typed as a list of GitHub repositories.
In this GitHubRepo class we'll describe what kind of data comes with a GitHub repository.
I've already prepared a little ListView, which we'll use for the display of the data.
Retrofit's heart is a class called "Retrofit".
In order to configure it easily, the developers added a fluent API
Next, we need to add a converter. You're going to use .addConverterFactory and going to pass a regular, standard Gson instance.
We got the builder, so it's time to create actual Retrofit objects.
The next step is to call an actual method on our client, which would be our reposForUser() and we're going to pass our own GitHub user name "fs-opensource".
The final step is to utilize this Call<> object.
Since you're in an activity, and thus in a UI thread, we need to do it async with the method called "enqueue".
The callback will be executed once we got the response from the server back.
We're going to use the response object and as you can see the body() is the List<GitHubRepo>
The final step is to pass the data to our ListView.
I'm excited to see if this actually worked!
Yes, we see all the GitHub repositories of our open source account.
Рекомендации по теме
Комментарии
Автор

Finally a more in depth look at retrofit!
When working at the scale and complexity of enterprise mobile, it is a necessity to understand how to dig in and customize our implementation.
So glad I found this...

matt-g-recovers
Автор

Entire playlist is Gold. Was a bit sceptical about changing from Jersey to Retrofit, this playlist made me stick with the decision.
Short and simple lectures with well defined goals that makes learning quiet easy. A good choice to use for Java REST calls irrespective of whether it is android or not. Just pointing out as most of the search results doesn't point Retrofit for J2EE applications.

quinoline
Автор

This man just has the best way to give tutorials, the way he explains everything, he know what to explain and what to not. Thanks for the tutorial
A request from my side is that if you can kindly speak every English word clearly, that would be best for the viewers whom don't getting your accent. Thanks

salmanmirza
Автор

Awesome example. This dude know what to explain and what not to explain in a tutorial.

fawzan
Автор

I am new to android, I appreciate your effort and valuable time putting in the tutorial and sharing your talent. Keep doing it.

kaushik
Автор

I wonder why people will dislike this video, thanks a lot. detailed explanations

lovethnwokike
Автор

Great video but it would be very helpful if you add English subtitles as well. It can be a little difficult to understand your accent for those of us unaccustomed to it.. I find myself going over things you say a few times to try to catch the words you say.

RonnieE
Автор

Great tutorial, but U forgot to explain the Adapter class

hithambasheir
Автор

Very nice, simple and useful tutorial! Congratulations!

lenoirzamboni
Автор

Really, I am very happy to watching retrofit tutorials, this help me soo

kasendentambwe
Автор

why did I buy the book? this free video is way informational!

alisheragzamov
Автор

Thank you so much for those tutorials, is hard to find great tutorials like this. Great teaching skills!!

asafeolimpio
Автор

Wow, very helpful tutorial - great video & audio quality.
Keep good job guys!

marekw
Автор

Thank you very much for explaining each & every point, awesome tutorial man, Cheers!!

subindsuresh
Автор

your tutorials were amazing ... i read Gson tutorials of yours its simply awesome ..

pawanbhardwaj
Автор

Nice tutorial, If someone needs retrofit but using Kotlin feel free to take a look. But this one is perfect if someone prefers Java

pragmaticcoding
Автор

You mentioned that "if you need asynchronous request you should use Call "
But documentation of Retrofit says:
"Each Call from the created GitHubService can make a synchronous or asynchronous HTTP request to the remote webserver."

sebuhi
Автор

video and tutorial are great, thanks for your work. (PS: could be better if has subtitles for non-english speaking learners ;-)

jinjidezaixiong
Автор

I really appreciate the content, thank you very much!

soonclass
Автор

Thank you so very much for this tutorial.

SurazVerma