Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2: @FormUrlEncoded with default fields

I need to send a request with application/x-www-form-urlencoded header. The response is list of some music albums in JSON format. There can be two optional parameter: total (default value = 5) and begin (default value = 0)

Here is the interface I use to send this request:

public interface MusicService {
    @Headers("Accept: Application/JSON")
    @FormUrlEncoded
    @POST("album/featured-albums")
    Call<List<Album>> listFeaturedAlbums(@Field("total") int total, @Field("begin") int begin);
}

The question is, how can I set default value to one or both of these fields, so that I don't have to send the parameters in each request. For example I want to get 30 items on every request and just play with begin field. Or maybe I want to use the default values for both fields:

public interface MusicService {
    @Headers("Accept: Application/JSON")
    @FormUrlEncoded
    @POST("album/featured-albums")
    Call<List<Album>> listFeaturedAlbums();
}

With this, I get an error:

java.lang.IllegalArgumentException: Form-encoded method must contain at least one @Field.

like image 830
Ashkan Sarlak Avatar asked Sep 09 '15 06:09

Ashkan Sarlak


1 Answers

At the moment I don't think you can do it in a non-hackish way this.

Guys at retrofit are working on adding this functionality: Allow specifying default @Field values. #951

You can follow this thread when it will complete. Or help them out and submit a PR :)

like image 76
Andrei Sfat Avatar answered Sep 28 '22 10:09

Andrei Sfat