Tran Triet Tran Triet - 2 months ago 9
reST (reStructuredText) Question

In Retrofit, why use one method per interface

I am new to android and REST client. I am using retrofit to build an app to communicate with an API.

Many tutorials said the best practice is to declare 1 method per interface (so many methods means many interfaces).

Examples: if I want 2 method, a GET and a POST. I will need two interfaces:

public interface GetService {
Call <ABC> getService();

public interface PostService {
Call<XYZ> postServer(@Body XYZ content);

and in the main_activity I will need to call

//call get
GetService get = ServiceGenerator.createService(GetService.class);
ABC call1 = get.getService();
//call post
PostService post = ServiceGenerator.createService(PostService.class);
XYZ call2 = post.postService();

Why can't I have only one interface like this:

public interface APIInterface {
Call <ABC> getService();

Call<XYZ> postServer(@Body XYZ content);

and in the Main_Activity, I only need to do the following:

APIInterface api = ServiceGenerator.createService(APIInterface.class);
ABC call1 = api.getService();
XYZ call2 = api.postServer();


You don't have to follow this "good practice" with one service per interface. If you use one interface per business part like below, it's ok.

public interface ProductRestService {
    Call <Product> getProduct(long id);

    Call <Product> update(long id, @Body Product product);

    Call <Product> create(@Body Product product);

    Call <Void> deleteProduct(long id);

The goal is just to avoid an interface with all your app's services