我的休息时间如下:
http://MY_SERVER/api/story?feed_ids=1,2,3&page=1
这里我应该提供以逗号分隔的动态 feed_ids列表, 因为我写了我的休息服务,如:
@GET("/story")
void getStory( @Query("feed_ids") List<Integer> feed_items, @Query("page") int page,Callback<StoryCollection> callback);
和
private List<Integer> items = Arrays.asList(1, 2, 3); // items is a list of feed ids subscribed by user (retrieved from app db). initialization is done here just for testing
public void getLatestStoryCollection(int page ,Callback<StoryCollection> callback) {
newsService.getStory(items, page ,callback);
}
我的代码运行正常,但改装发送请求网址如:
http://MY_SERVER/api/story?feed_ids=1&feed_ids=2&feed_ids=3&page=1
有没有办法发送这样的动态参数列表,就像feed_ids=1,2,3
一样,没有重复的参数名称?
答案 0 :(得分:2)
您可以创建一个覆盖toString()
的自定义类,将它们格式化为逗号分隔列表。类似的东西:
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);
答案 1 :(得分:1)
在改造中没有办法做到这一点,但你可以自己轻松地做到这一点。由于您使用的是android,因此可以使用TextUtils.join()
将任何列表转换为String。然后将该字符串传递给您的查询参数而不是列表。首先,更新您的界面以取String
而不是List
。
@GET("/story")
void getStory( @Query("feed_ids") String feed_items, @Query("page") int page, Callback<StoryCollection> callback);
然后当您拨打getStory
方法时,先将项目通过join
-
String items = TextUtils.join(",", Arrays.asList(1, 2, 3));
newsService.getStory(items, page, callback);