S
S
Sergey Semenko2017-03-22 22:43:15
Java
Sergey Semenko, 2017-03-22 22:43:15

Retrofit 2. How to set value for Boolean parameters?

There is a simple service:

public interface CatalogService
{
    @GET("catalog")
    Observable<PaginationData<CatalogItem>> paginate(
            @Query("category") int category,
            @Query("favorite") boolean favorite,
            @Query("page") int page
    );
}

The result is something like the following query:
http://example.com/api/v1/catalog?category=1&favorite=true&page=2

I want favorite to be replaced with 1 (if true) or 0 (if false). How can I do that?
Now the output is a true/false string, which is not very convenient. Is it really necessary to write everywhere isFavorite() ? 1 : 0?
PS I'm not lazy, I just want a beautiful solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Gamega, 2017-03-23
@gadfi

I'm sorry, but what's the beauty? a number in the url, it is logical that the service will also have a number, this will avoid confusion
If you have a model that has a boolean favor property, add a method to the model

int getFavor(){
    return favor ?1:0;
}

and don’t get confused, the exact display of the url in the service avoids problems and confusion

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question