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.
Read the full tutorial for easy code copy&pasting here: ►https://futurestud.io/tutorials/retro...
GitHub Repository for the code:
►https://github.com/futurestudio/andro...
Watch 20+ Retrofit videos in our playlist here:
► • Android Retrofit Tutorial Series
----------------------------------------
Our book on Retrofit is available on leanpub:
►https://leanpub.com/retrofit-love-wor...
----------------------------------------
Future Studio is helping 5,000+ users daily to solve Android and Node.js problems with 320+ written tutorials and videos. We’re on a mission to provide new in-depth content every week.
Checkout 320+ technical in-depth tutorials:
►https://futurestud.io
Subscribe for two new videos every week:
►https://www.youtube.com/c/FutureStudi...
----------------------------------------
Follow us on social media to get updates on new content:
► / futurestud_io
► / futurestudiouniversity
►https://plus.google.com/+FutureStudio...
----------------------------------------
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.
Before we start describing our API endpoints, you should check the AndroidManifest.xml if we actually have the permission to do network requests.
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
with the Retrofit.Builder. One thing you usually have to specify is the base URL. For GitHub this is https://api.github.com/
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.
We can use retrofit.create() for that and we're just going to pass our GitHubClient class.
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".
We'll say call.enqueue() and it expects a callback.
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.
Смотрите видео Retrofit Tutorial — Getting Started and Creating an Android Client онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Future Studio 19 Январь 2017, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 140,578 раз и оно понравилось 1.3 тысяч людям.