MVP + Dagger2 + Retrofit (and Swagger)

Today, I’m going to explain how to create a sample project using MVP + Dagger2 + Retrofit.

Go to RetroDagger example on Github

such face, wow

Don’t panic if you don’t know nothing (Jon Snow) about those three things. Click in this three links to enjoy: MVP, Dagger2, Retrofit

Tip: Migration Dagger 1 to Dagger 2

The main and new part in this combo is Retrofit. We can request the webservices of a REST api with POST, GET and more. This library is awesome and very useful. It is a MUST in all your codes.

So, we have created our structure with MVP, also injected all dependencies with Dagger2… mmm… something is missing…

Yes, we need to retrieve some data to our code. In this case, we are going to use Swagger. It is a simple yet powerful representation of your RESTful API.

First of all, we must create a powerful model…

public class PetModel {
private String name;
public String getName() {return name;}

In this model, all its attributes must be equal to the name in Swagger. I only want to show in app the name of the pets, so I have decided to include in my model the attribute “name” but if you want to retrieve more data you can add more to your model.

In our Api class, we have the next code:

“Content-type: application/json”
public void getPets(Callback<List<PetModel>> callback);

It’s very easy, put the content type in your header (Json), your method (GET), your link extension (pet/findByStatus?status=available) and the callback model (List<PetModel>).

Finally, to retrieve all the data we must do a call to our api with a RestAdapter in our Interactor implementation.

RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Constants.URL) //
MyApi getPetsName = restAdapter.create(MyApi.class);
getPetsName.getPets(new Callback<List<PetModel>>() {
public void success(List<PetModel> petModelList, Response response) {
public void failure(RetrofitError retrofitError) {

And That’s All Folks! ☺