相关疑难解决方法(0)

在RxJava 2.x中使用flatMap

我正在使用自己的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",
                },
                ...
                ]
        }
}
Run Code Online (Sandbox Code Playgroud)

现在,我正在使用改造这样的前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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后:

public interface MyService  {
    @GET( "/users")
    Observable<DataResponse> getUsers(
            @Query("cursor") String cursor,
    );
}
Run Code Online (Sandbox Code Playgroud)

MyService service = RetrofitClient.getInstance();
service.getUsers() …
Run Code Online (Sandbox Code Playgroud)

pagination android retrofit flatmap rx-java2

4
推荐指数
1
解决办法
1078
查看次数

标签 统计

android ×1

flatmap ×1

pagination ×1

retrofit ×1

rx-java2 ×1