MobileUI LogoMobileUI
/

🔌 Retrofit (REST Client)

Elegantly consuming REST APIs


The Retrofit Plugin PURPLE adds dependency management and metadata for the popular Retrofit Library to your project. While you could actually add Retrofit dependencies on your own, this plugin adds additional metadata for RoboVM and Android, making Retrofits's integration much easier. Additionally, we have tested the linked version of Retrofit with the rest of the MobileUI Framework libraries and confirm compatibility with all current MobileUI components.

Plugin Infos
Dependency"io.nevernull:mobileui-plugin-retrofit"
Api DocsOfficial Retrofit Documentation
Main InterfaceOfficial Retrofit Documentation

Using the Retrofit dependencies

Retrofit can be integrated differently, depending on the converter for the JSON response you are planning to use. You can find all possible combinations in the Retrofit Documentation. To make the integration of Retrofit into your MobileUI project easier, you should use MobileUI's Dependency Management by adding a platform dependency as shown below. You can then add your required retrofit dependencies with a MobileUI compatible version by just omitting all version information:

app-common/build.gradle
dependencies {
    api platform("io.nevernull:mobileui-platform:$mobileuiVersion")

    // Use the Retrofit dependency without concrete version
    api "com.squareup.retrofit2:converter-jackson"
    ...
}

Example Usage

Here is a short example on how we propose to use the Retrofit libraries.

1. Define your data class

Objects of this class are created by Retrofit when deserializing the JSON response.

MainController.java
public class Todo {
    public long id;
    public long userId;
    public String title;
    public boolean completed;}

2. Define your Retrofit-based Service

For Retrofit, you define the API of your web service via an interface and different annotations as exemplified below:

MainController.java
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface TodoService {
    @GET("todos/{id}")
    Call<Todo> getTodoById(@Path("id") long id);
}

3. Configure the Service Singleton

Use MobileUI Inject to configure and define the service as Singleton for your app. This lets you inject it everywhere later on.

MainController.java
@Factory
public class Config {

    @Singleton
    public TodoService todoService(OkHttpClient client) {
        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl("https://jsonplaceholder.typicode.com/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        return retrofit.create(TodoService.class);
    }
}

4. Inject and use the Service in your Controller

To use your service, use constructor injection in your managed beans and Retrofit's Call API to make asynchronous (non-main thread) calls as shown below:

RetrofitController.java
@Prototype
public class RetrofitController {

    private TodoService todoService;

    public volatile Todo todo;

    public RetrofitController(TodoService todoService) {
        this.todoService = todoService;
    }

    public void onGetTodoClicked() {
        Call<Todo> todoById = todoService.getTodoById(1);
        todoById.enqueue(new Callback<Todo>() {
            @Override
            public void onResponse(Call<Todo> call, Response<Todo> response) {
                todo = response.body();
                MobileUI.firePropertyChanged(RetrofitController.this, "todo");
            }

            @Override
            public void onFailure(Call<Todo> call, Throwable e) {
                e.printStackTrace();
            }
        });
    }

}

You can use the call MobileUI.firePropertyChanged(...) from the callbacks without problem. Please make sure, that all fields that are changed are volatile.

Further Reading