8
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Android】RetrofitのGETリクエストで配列を渡したい

Last updated at Posted at 2016-02-18

サーバー側で、Parameters: {"hoges"=>["1", "2", "3"]} みたいな感じで、配列を受け取りたい。
POSTやPUTだと、

@Field("hoges[]") List<String> hoges

とできるが、GETだとできない。公式にものっていない。

https://github.com/square/retrofit/issues/472
Jake本人による説明があった。
インターフェースで可変長引数を使えば良い。

インターフェース側
@GET("/hoges.json")
    public Observable<Hoges> getHoges(@Query("hoges[]") String... hoges);
呼び出し側
String[] hoges = {"1", "2", "3"};
observable = restAdapter.create(RetrofitApi.class)
                    .getHoges(hoges);
生成されるリクエスト
/hoges.json?hoges[]=1&hoges[]=2&hoges[]=3
8
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?