0
votes

I have two methods that return an Observable< Response< ResponseBody > >

firstAPI.getFirstInfo("1", "2");

secondApi.getSecondInfo(resultFirstObservable, "3");

I'm trying to get an information from the first one and use it as a parameter in the second one.

I started by trying to use a flatMap:

Observable<Integer> mergedObservers = firstAPI.getFirstInfo("1","2")
    .flatMap((Response<ResponseBody> resultFirstObservable) -> {
        try {
            return secondApi.getSecondInfo(transformToTheFormatNeeded(resultFirstObservable.body().string()), "3");
        } catch (IOException e) {
            e.printStackTrace();
            return secondApi.getSecondInfo("defaultValue", "3");
        }
    }, ((Response<ResponseBody> resultFirstObservable), (Response<ResponseBody> resultSecondObservable)) -> {
        try {
            return transformToWhatINeed(resultSecondObservable.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    });

I would try then to subscribeOn this new Observable and onNext I would send the returned value to my Activity.

Subscription sub = mergedObservers.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .repeatWhen(completed -> completed.delay(30, TimeUnit.SECONDS))
            .subscribe(new Observer<Integer>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(Integer integer) {
                    view.updateInfo(integer);
                }
            });

My problem is that the flatMap throws 2 errors at compilation:

  1. error: lambda body is neither value nor void compatible

  2. error: no suitable method found for flatMap((__)->{ tr[...]; } },(.. ,..)->{ [...]; } })

What am I doing wrong ?

1
when you have compile errors with lambdas, your types are wrong, please "expand lambdas to anonymous class" to get the full pictures, update the code so that everyone can helpericn

1 Answers

2
votes

I don't think the API call actually throws an IOException while also returning an Observable. In addition, you have to return something from the second lambda but the try-catch there doesn't do that, causing the error. Try this:

Observable<Integer> mergedObservers = firstAPI.getFirstInfo("1","2")
.flatMap(resultFirstObservable -> {
    return secondApi.getSecondInfo(resultFirstObservable, "3")
        .onErrorResumeNext(e -> {
             e.printStackTrace();
             return secondApi.getSecondInfo("defaultValue", "3");
        });
}, (resultFirstObservable, resultSecondObservable) -> {
    try {
        return transformToWhatINeed(resultSecondObservable.body().string());
    } catch (IOException ex) {
        ex.printStackTrace();
        return "";
    }
});