我正在使用retrofit2和rxjava2按顺序进行多个api调用。我发出请求以获取列表元素。之后,我需要为每个对象制作一个请求序列(不是并行)。
使用递归函数解决
答案 0 :(得分:0)
如果您只想执行数组的每个项目。只需使用Observable.just()
或Observable.fromIterable()
List<String> list = Arrays
.asList("One", "Two", "Three", "Four", "Five");
Observable<String> observable = Observable.from(list);
observable.subscribe(new Subscriber<String>() {
public void onStart() {
System.out.println("onStart");
}
public void onCompleted() {
System.out.println("Completed!");
}
public void onError(Throwable e) {
System.out.println("Exception thrown: " + e);
}
public void onNext(String s) {
System.out.println("Next element: " + s);
}
});
结果是
onStart
Next element: One
Next element: Two
Next element: Three
Next element: Four
Next element: Five
Completed!