DEV Community

Lody G Mtui
Lody G Mtui

Posted on

Retrofit Workflow

Retrofit is a type-safe REST client library that is used to consume RESTful Web Services.

Add Dependency
Go to Retrofit, copy the following line implementation 'com.squareup.retrofit2:retrofit:(insert latest version)'

To get the latest version, navigate to Square Github.

Retrofit works with different serialization/deserialization library such as Moshi,Gson etc.In this case, we use Gson.

From the same page, Retrofit, copy this line com.squareup.retrofit2:converter-gson and add the latest version as Retrofit.

Then add the dependencies in the build.gradle.kts as follows:-

dependencies {

    implementation("com.squareup.retrofit2:converter-gson:2.11.0")

    implementation("com.squareup.retrofit2:retrofit:2.11.0")


}
Enter fullscreen mode Exit fullscreen mode

Create Dataclass/Modal class

public class Movie {
    @SerializedName("id")
    @Expose
    private Integer id;

    @SerializedName("overview")
    @Expose
    private String overview;

    @SerializedName("poster_path")
    @Expose
    private String posterPath;

    @SerializedName("release_date")
    @Expose
    private String releaseDate;

    @SerializedName("title")
    @Expose
    private String title;

    @SerializedName("vote_average")
    @Expose
    private Double voteAverage;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getOverview() {
        return overview;
    }

    public void setOverview(String overview) {
        this.overview = overview;
    }

    public String getPosterPath() {
        return posterPath;
    }

    public void setPosterPath(String posterPath) {
        this.posterPath = posterPath;
    }

    public String getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(String releaseDate) {
        this.releaseDate = releaseDate;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Double getVoteAverage() {
        return voteAverage;
    }

    public void setVoteAverage(Double voteAverage) {
        this.voteAverage = voteAverage;
    }
}

Enter fullscreen mode Exit fullscreen mode

Create Interface class

public interface MovieApi {
    @GET("movie/popular")
    Call<Result> getPopularMovies(@Query("api_key") String apiKey);

}
Enter fullscreen mode Exit fullscreen mode

Create Retrofit Instance

public class RetrofitInstance {
    private static Retrofit retrofit = null;
    private final static String BASE_URL ="https://api.themoviedb.org/3/";

    public static MovieApi getApi(){
        if (retrofit == null){
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit.create(MovieApi.class);
    }

}
Enter fullscreen mode Exit fullscreen mode

Create Repository (Optional)

public class MovieRepository {
    private ArrayList<Movie> movies = new ArrayList<>();


    public MovieRepository(Application application) {
        this.application = application;
    }

    public MutableLiveData<List<Movie>> getMutableLiveData(){
        MovieApi movieApi = RetrofitInstance.getApi();

        Call<Result> call = movieApi.
                getPopularMovies(application.getApplicationContext().getString(R.string.api_key));


        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                Result result = response.body();

                if (result != null && result.getResults() != null){
                    movies = (ArrayList<Movie>) result.getResults();
                    mutableLiveData.setValue(movies);
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable throwable) {

            }
        });
        return mutableLiveData;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)