Here's my code
final ApiInterface apiInterface=restAdapter.create(ApiInterface.class);
apiInterface.submitDataToAnalyze("dataToAnalyze","852741963",1,"123","lalala","2015-11-20")
.flatMap(new Func1<BasicResponse, Observable<?>>() {
@Override
public Observable<?> call(BasicResponse basicResponse) {
if (basicResponse.getResult() == 1){
return apiInterface.getSuggestion("dataToAnalyze","852741963",1,"123","lalala","2015-11-20");
}else{
return null; //error
}
}
}).flatMap(new Func1<Observable<?>, Integer>() {
@Override
public Integer call(Observable<?> input) {
if (input == null){
//update a table
return 0;
}else{
//update another table using data retrieved from apiInterface.getSuggestion
return 1;
}
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
What should I do, so that when the call completes, either at
return apiInterface.getSuggestion()
or
return Observable.just(null)
Two different function (one for each case) would be called? And of course, the function need to work with the data returned from the call.
Btw the second flatMap returns this error:
'flatMap(rx.functions.Func1<? super java.lang.Object,? extends rx.Observable<?>>)' in 'rx.Observable' cannot be applied to '(anonymous rx.functions.Func1<rx.Observable<?>,java.lang.Integer>)'
If it helps, here's my interface for submitDataToAnalyze and getSuggestion
@POST("/request.php")
Observable<BasicResponse> submitDataToAnalyze(@Query("action") String action, @Query("auth") String auth, @Query("ver") int ver, @Query("uuid") String uuid, @Query("dataToAnalyze") String dataToAnalyze,
@Query("requestForDate") String requestForDate);
@POST("/request.php")
Observable<BasicResponse> getSuggestion(@Query("action") String action, @Query("auth") String auth, @Query("ver") int ver, @Query("uuid") String uuid, @Query("dataToAnalyze") String dataToAnalyze,
@Query("requestForDate") String requestForDate);