filmov
tv
Retrofit Tutorial — Getting Started and Creating an Android Client
![preview_player](https://i.ytimg.com/vi/R4XU8yPzSx0/maxresdefault.jpg)
Показать описание
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.
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.
Комментарии