简体   繁体   English

使用参数中第一个的结果将两个Observable与第二个

[英]Combine two Observables with the second using results from the first one in parameters

I have two methods that return an Observable< Response< ResponseBody > > 我有两种方法返回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: 我首先尝试使用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. 然后,我将尝试在这个新的Observable和onNext上进行订阅,然后将返回的值发送到我的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: 我的问题是,flatMap在编译时会引发2个错误:

  1. error: lambda body is neither value nor void compatible 错误:lambda主体既不兼容值也不兼容void

  2. error: no suitable method found for flatMap((__)->{ tr[...]; } },(.. ,..)->{ [...]; } }) 错误:找不到适用于flatMap((__)-> {tr [...];}},(..,..)-> {[...];}})的合适方法

What am I doing wrong ? 我究竟做错了什么 ?

I don't think the API call actually throws an IOException while also returning an Observable. 我不认为API调用实际上会引发IOException并返回Observable。 In addition, you have to return something from the second lambda but the try-catch there doesn't do that, causing the error. 另外,您必须从第二个lambda返回某些内容,但是那里的try-catch却没有这样做,从而导致错误。 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 "";
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Observable 与第二个 Observable 结合使用,后者使用第一个 Observable 的结果 - Combine Observable with second Observable which uses results from from the first one 合并两个可观察对象,对其中之一进行操作 - Combine two observables, act on one of them Java:使用第一个数组的前3个整数,然后是第二个数组的3个整数,将2个数组组合成第三个数组 - Java: Combine 2 arrays into a third array using the first 3 integers of the first array then 3 integers from the second one RxJava-结合两个Observable - RxJava - Combine two Observables 由于引擎下的SQLBrite,在不使用toList()的情况下组合两个observable - Combine two observables without using toList() because of SQLBrite under the hood 如何结合两个ListenableFutures并从第一个中获得结果以完成 - How combine two ListenableFutures and get the result from the first one to complete 将两个 Mono 组合在一起,其中第二个 Mono 订阅了第一个 - Combine two Mono's together where the second mono is subscribed to the first one 有没有办法将两个列表合并到第一个列表中的每个子列表中,其中第二个列表元素? - Is there a way to combine two list which will have second list element into each and every sublist in first one? RxJava:结合两个可选的observable - RxJava: combine two optional observables 试图将两个数组组合成一个排序的数组; 第一个数组小于第二个数组时,代码不起作用 - Trying to combine two arrays into one sorted array; code does not work when first array is smaller than second array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM