首先,如果您使用RxJava2和Retrofit2,请将 Observable 替换为 Single . Single 的结果恰好是一个 onSuccess() 或一个 onError() 方法调用 . Observable 更通用 .
public void func() {
Disposable disposable = Single.concat(getOrganisationList(), getStaffList())
.subscribe(result -> {
// called twice, when organisation call succeeded and when staff call succeeded
}, throwable -> {
// handle exceptions you want
if (throwable instanceof NullPointerException) {
// organisation - empty or null; staff - null
} // else if(throwable instanceof IOException) ...
});
}
public Single> getOrganisationList() {
return Single.just(Collections.singletonList("Hello")) // (1)
.map(organizations -> {
if (organizations.isEmpty()) {
throw Exceptions.propagate(new NullPointerException("List is empty"));
}
return organizations;
}).doOnSuccess(organizations -> {
// do something with organizations list
});
}
public Single> getStaffList() {
return Single.just(Collections.emptyList()) // (2)
.doOnSuccess(organizations -> {
// do something with staff list
});
}
您应该将API调用添加到标记为(1)和(2)的位置 . 我使用 String 和 Integer 来代替您的泛型类型_1120014_,以确保可以编译代码 .