我有 2 个改装电话需要进行 A 和 B:
(A):返回一个 ArrayList
(B):获取 (A) 的结果,它是一个 ArrayList。(B) 遍历 ArrayList 并使用每个进行改造调用,并将结果数据组合成最终的 ArrayList,该 ArrayList 将发送给我的订阅者 onNext()
我能够让它与平面图一起使用,但解决方案不是很优雅。据我了解,更好的解决方案是将 flatMapIterable 与 concatMap 一起使用,但我似乎无法采用我对 flatMapIterable 和 concatMap 所做的工作。
ServiceFactory.createRetrofitService().getUserFollowing(sessionMgr.getAuthToken())
.flatMap(new Func1<UserSelfFollows, Observable<? extends ArrayList<Media.MediaData>>>() {
@Override
public Observable<? extends ArrayList<Media.MediaData>> call(UserSelfFollows userSelfFollows) {
//make second requests based on response from First request to get all Users
ArrayList<Media.MediaData> arAllMedia = new ArrayList<>();
for(UserSelfFollows.UserDataFollows user : userSelfFollows.userdata){
Response <ResponseBody> response ;
Call <ResponseBody> call;
try {
call = ServiceFactory.createRetrofitService().getMediaOfUser(user.id,sessionMgr.getAuthToken());
response = call.execute();
}catch(IOException ex){
return Observable.error(ex);
}
if (response.isSuccessful()) {
try {
String str = responseHelper.streamToString( response.body().byteStream());
Gson gson = new GsonBuilder().create();
Media media = gson.fromJson(str, Media.class);
arAllMedia.addAll(media.mediaData);
} catch (IOException e) {
return Observable.error(e);
}
} else {
return Observable.error( new Exception( responseHelper.getErrorString( response.errorBody().byteStream())) );
}
}
return Observable.just(arAllMedia);
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ArrayList<Media.MediaData>>() {
@Override
public final void onCompleted() {
}
@Override
public final void onError(Throwable e) {
}
@Override
public final void onNext(ArrayList<Media.MediaData> arMedia) {
}
})
这是我到目前为止所拥有的,但它不会编译:
ServiceFactory.createRetrofitService().getUserFollowing(sessionMgr.getAuthToken())
.flatMapIterable(new Func1<UserSelfFollows, Iterable<?>>() {
@Override
public Iterable<?> call(UserSelfFollows userSelfFollows) {
return userSelfFollows.userdata;
}
})
.<Media.MediaData>flatMap(new Func1<UserSelfFollows.UserDataFollows, Observable<Media.MediaData>>() {
@Override
public Observable<Media.MediaData> call(UserSelfFollows.UserDataFollows user) {
return ServiceFactory.createRetrofitService().getMediaOfUser(user.id, sessionMgr.getAuthToken());
}
})
.toList();
错误是: