我在使用自己的API并希望使用RxJava链式调用一些分页结果。我使用基于游标的分页(假设第一个请求中有50个用户):
{
"data":{
"status":"ok",
"total":988, //users total
"has_next_page":true,
"end_cursor":"AQAxd8QPGHum7LSDz8DnwIh7yHJDM22nEjd",
"users":[{"id":"91273813",
"username":"codergirl",
"full_name":"Code Girl",
"picture_url":"https://cdn.com/21603182_7904715668509949952_n.jpg",
},
...
]
}
}
现在,我正在使用retrofit获取前50个结果,如下所示:
public class DataResponse {
@SerializedName("end_cursor")
private String end_cursor;
@SerializedName("users")
private JsonArray users;
@SerializedName("has_next_page")
private Boolean has_next_page;
public boolean hasNextCursor(){
return has_next_page;
}
public String endCursor(){
if (hasNextCursor()){
return end_cursor;
}
return "";
}
public JsonArray getUsers(){
return users;
}
}
然后:
public interface MyService {
@GET( "/users")
Observable<DataResponse> getUsers(
@Query("cursor") String cursor,
);
}
并且
MyService service = RetrofitClient.getInstance();
service.getUsers()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe( val-> showUsers(val.getUsers())); // getting the first 50 users
下一步调用应该是 "/users?cursor=AQAxd8QPGHum7LSDz8DnwIh7yHJDM22nEjd",我想返回所有的用户(在本例中为988)。