Professional Documents
Culture Documents
Retrofit is a REST Client for Java and Android. It makes it relatively easy to retrieve and
upload JSON (or other structured data) via a REST based webservice. In Retrofit you
configure which converter is used for the data serialization. Typically for JSON you use
GSon, but you can add custom converters to process XML or other protocols. Retrofit uses
the OkHttp library for HTTP requests.
Compare with Volley and AsyncTask, Retrofit providing the very fast response to the
request.
5 step to create retrofit android example
1. Add the retrofit dependency
2. Creating Model Class
3. Create The Retrofit Instance
4. Setting Up The Retrofit Interface
5. Consume The REST Web Service
6. Set Retrofit response data into the Recyclerview
Then, We have to add the retrofit dependency into our build.grade(app : level) file. We can
find the latest retrofit version in the official retrofit website. http://square.github.io/retrofit/
And the GSON converter from retrofit used to convert the JSON response from the server.
implementation 'com.squareup.retrofit2:converter-gson:latest.version'
[
{
"id":"1", "image":"http://velmm.com/images/bottom_navigationview/coco.jpg",
"title":"Coco"
},
{
"id":"2",
"image":"http://velmm.com/images/bottom_navigationview/terminator_2.jpg",
"title":"Terminator 2: Judgment Day 3D"
},
{
"id":"3", "image":"http://velmm.com/images/bottom_navigationview/dunkirk.jpg",
"title":"Dunkirk"
},
{
"id":"4", "image":"http://velmm.com/images/bottom_navigationview/the_salesman.jpg",
"title":"The Salesman"
},
{
"id":"5", "image":"http://velmm.com/images/bottom_navigationview/lion.png",
"title":"Lion"
}
]
In my JSON response, I am having the list of movies with name, year and director properties.
So, My Model class will be like Movie as class name and name, year, director are properties.
Movie.java
class Movie {
@SerializedName("title")
private String title;
@SerializedName("image")
private String imageUrl;
@SerializedName is used to map the POJO object into to JSON response properties.
ApiClient.java
Because the POJO classes are wrapped into a typed Retrofit Call class.
Method Parameters :
@Query – We can simply add a method parameter with @Query() and a query parameter
name, describing the type.
ApiInterface.java
@GET("volley_array.json")
Call<List<Movie>> getMovies();
}
After the initialization, we to call the getMovies() interface and implement the CallBacks.
Part of the Implementation we need to override the onResponse() and onFailure().
If the request succeeds the callback will come into onResponse(). If any error in the request
the callback will go into onFailure() method. In onResponse() method, we can get our
response from response body.
@Override
public void onFailure(Call<List<Movie>> call, Throwable t) {
Log.d("TAG","Response = "+t.toString());
}
});
Now our data is received in onReceive() . We can use the data for our application purpose. In
this example, I am using this retrofit data to setup in recyclerview.
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<List<Movie>> call = apiService.getMovies();
call.enqueue(new Callback<List<Movie>>() {
@Override
public void onResponse(Call<List<Movie>> call,
Response<List<Movie>> response) {
movieList = response.body();
Log.d("TAG","Response = "+movieList);
recyclerAdapter.setMovieList(movieList);
}
@Override
public void onFailure(Call<List<Movie>> call, Throwable t) {
Log.d("TAG","Response = "+t.toString());
}
});