我正在使用 Spotify API,并希望使用 RxJava 链接一些分页结果。 Spotify 使用基于光标的分页,因此解决方案如下来自@lopar 的一个 https://stackoverflow.com/a/29594194/377260不管用。
回复来自这个电话 https://developer.spotify.com/web-api/get-followed-artists/看起来像这样(想象一下有 50 个items
):
{
"artists" : {
"items" : [ {
"id" : "6liAMWkVf5LH7YR9yfFy1Y",
"name" : "Portishead",
"type" : "artist"
}],
"next" : "https://api.spotify.com/v1/me/following?type=artist&after=6liAMWkVf5LH7YR9yfFy1Y&limit=50",
"total" : 119,
"cursors" : {
"after" : "6liAMWkVf5LH7YR9yfFy1Y"
},
"limit" : 50,
"href" : "https://api.spotify.com/v1/me/following?type=artist&limit=50"
}
}
现在,我使用改造得到了前 50 个这样的结果:
public class CursorPager<T> {
public String href;
public List<T> items;
public int limit;
public String next;
public Cursor cursors;
public int total;
public CursorPager() {
}
}
public class ArtistsCursorPager {
public CursorPager<Artist> artists;
public ArtistsCursorPager() {
}
}
then
public interface SpotifyService {
@GET("/me/following?type=artist")
Observable<ArtistsCursorPager> getFollowedArtists(@Query("limit") int limit);
@GET("/me/following?type=artist")
Observable<ArtistsCursorPager> getFollowedArtists(@Query("limit") int limit, @Query("after") String spotifyId);
}
and
mSpotifyService.getFollowedArtists(50)
.flatMap(result -> Observable.from(result.artists.items))
.flatMap(this::responseToArtist)
.sorted()
.toList()
.subscribe(new Subscriber<List<Artist>>() {
@Override
public void onNext(List<Artist> artists) {
callback.onSuccess(artists);
}
// ...
});
我想返回所有(在本例中为 119)艺术家callback.success(List<Artist>)
。我是 RxJava 新手,所以我不确定是否有smart方法来做到这一点。