我建议使用平面地图(如果可以的话,也可以使用retrolambda)。另外,
Single<FirstResponse>first如果您不对返回值进行任何操作,则无需保留返回值(例如)。
retrofitService.getSomething() .flatMap(firstResponse -> retrofitService.getSecondResponse(firstResponse.id) .subscribeWith(new DisposableSingleObserver<SecondResponse>() { @Override public void onSuccess(final SecondResponse secondResponse) { // we're done with both! } @Override public void onError(final Throwable error) { // a request request Failed,} });这文章让我觉得通过我如何总体结构RxJava风格。如果可能的话,您希望您的链条是高级操作的列表,以便可以将其理解为一系列操作/转换。
编辑 没有lambdas,您可以只
Func1为您的flatMap使用。做同样的事情只是增加了很多样板代码。
retrofitService.getSomething() .flatMap(new Func1<FirstResponse, Observable<SecondResponse> { public void Observable<SecondResponse> call(FirstResponse firstResponse) { return retrofitService.getSecondResponse(firstResponse.id) } }) .subscribeWith(new DisposableSingleObserver<SecondResponse>() { @Override public void onSuccess(final SecondResponse secondResponse) { // we're done with both! } @Override public void onError(final Throwable error) { // a request request Failed,} });


