我从使用 RxAndroid 的 Rx 开始,我想通过其 API 和 Retrofit 列出 GitHub 用户的名称。每个 API 请求带来 100 个用户,在我处理完所有用户之后,我想获得下一个 100 个用户,但我不知道该怎么做。任何人都可以帮助我吗?
我的一些代码片段在哪里
改造界面
interface GitHubService {
@GET("/users")
List<User> users(@Query("since") int since);
}
用户类
class User {
private Integer id;
private String login;
public String getLogin() {
return login;
}
public Integer getId() {
return id;
}
}
还有我活动的 onCreate 方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("User-Agent", "Retrofit-Sample-App");
request.addHeader("Accept", "application/json");
}
};
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.github.com")
.setRequestInterceptor(requestInterceptor)
.build();
GitHubService mWebService = restAdapter.create(GitHubService.class);
mWebService.users().flatMap(new Func1<List<User>, Observable<User>>() {
@Override public Observable<User> call(List<User> users) {
return Observable.from(users);
}
}).flatMap(new Func1<User, Observable<String>>(){
@Override public Observable<String> call(User user) {
return Observable.just(user.getId() + ": " + user.getLogin());
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
Log.i("TEST", "EXIT");
}
@Override
public void onError(Throwable e) {
Log.i("TEST", e.getLocalizedMessage());
}
@Override
public void onNext(String s) {
Log.i("TEST", s);
}
});
}