Android 没有重复参数名称的参数动态列表
我的rest api如下所示:Android 没有重复参数名称的参数动态列表,android,retrofit,Android,Retrofit,我的rest api如下所示: http://MY_SERVER/api/story?feed_ids=1,2,3&page=1 这里我应该提供由逗号分隔的提要ID的动态列表, 为此,我编写了我的rest服务,如: @GET("/story") void getStory( @Query("feed_ids") List<Integer> feed_items, @Query("page") int page,Callback<StoryCollection>
http://MY_SERVER/api/story?feed_ids=1,2,3&page=1
这里我应该提供由逗号分隔的提要ID的动态列表,
为此,我编写了我的rest服务,如:
@GET("/story")
void getStory( @Query("feed_ids") List<Integer> feed_items, @Query("page") int page,Callback<StoryCollection> callback);
有没有一种方法可以发送这样的动态参数列表,就像
feed\u ids=1,2,3
,而不需要重复的参数名称?在改型中没有这种方法,但您可以自己轻松地完成。由于您使用的是android,因此可以使用将任何列表转换为字符串。然后将该字符串作为查询参数而不是列表传递给。首先,更新您的界面以获取字符串
,而不是列表
@GET("/story")
void getStory( @Query("feed_ids") String feed_items, @Query("page") int page, Callback<StoryCollection> callback);
您可以创建一个自定义类,该类重写
toString()
,将它们格式化为逗号分隔的列表。比如:
class FeedIdCollection extends List<Integer> {
public FeedIdCollection(int... ids) {
super(Arrays.asList(ids));
}
@Override
public String toString() {
return TextUtils.join(",", this);
}
}
class FeedIdCollection扩展了列表{
公共FeedIdCollection(int…ids){
super(Arrays.asList(ids));
}
@凌驾
公共字符串toString(){
返回TextUtils.join(“,”,this);
}
}
然后做出声明:
@GET("/story")
void getStory( @Query("feed_ids") FeedIdCollection feed_items, @Query("page") int page, Callback<StoryCollection> callback);
@GET(“/story”)
void getStory(@Query(“feed_id”)FeedIdCollection feed_items,@Query(“page”)int page,回调);
这已经不起作用了。它将扩展到实现,并且super接受0个参数。
String items = TextUtils.join(",", Arrays.asList(1, 2, 3));
newsService.getStory(items, page, callback);
class FeedIdCollection extends List<Integer> {
public FeedIdCollection(int... ids) {
super(Arrays.asList(ids));
}
@Override
public String toString() {
return TextUtils.join(",", this);
}
}
@GET("/story")
void getStory( @Query("feed_ids") FeedIdCollection feed_items, @Query("page") int page, Callback<StoryCollection> callback);