T
T
trukhachev2017-04-16 16:33:00
Android
trukhachev, 2017-04-16 16:33:00

How to execute requests to 2 different servers and get the same result?

I understand with rxjava in practice and I ran into a problem that I can not solve. The bottom line is that you need to execute 2 requests to different services asynchronously (android application). It is important that the response from 2 services can be 400, but at the same time it is important for me not to lose the response from 1 service.
I tried this, the bad thing about this solution is that if one server does not respond, I will lose the response from the other. How to solve it correctly?

Observable
                        .zip(serv1Observable,
                                serv2Observable,
                                new BiFunction<Responce1, Responce2, Responce3>() {
                                    @Override
                                    public Responce3 apply(@NonNull Responce1 r1, @NonNull Responce2 r2) throws Exception {
                                        return new Responce3(r1, r2);
                                    }
                                })
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<Responce3>() {
                            @Override
                            public void accept(@NonNull Responce3 r3) throws Exception {

                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(@NonNull Throwable throwable) throws Exception {

                            }
                        });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexeyVD, 2017-04-17
@trukhachev

Error Handling Operators
If I understand you correctly, then you want, in case of an error in the request to the second service, to still forward the result. As an option, add an onErrorReturn() operator to the Observable chain of the second service, handle the error in it and pass on an empty result or something like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question