1

I want to call multiple Rest Api's in a Sequence and having each Response Dto is different from each other.

Please help me to get rid from this situation that, How can i call these Api's using Rx Java Observables in Android.

2
  • you can use concat operator which takes multiple Observables and concatenates their sequences Commented Sep 17, 2017 at 5:11
  • Thanks, This solution is helpful for me. Commented Sep 17, 2017 at 7:19

4 Answers 4

1

no, you should use map() or doOnNext(), it will look like this

Observable.just(1)
    .doOnNext(value -> {
        someRequestX().execute();    
    })
    .map(value -> {
        return nextRequestY().execute();    
    })
    .doOnNext(requestYResponse-> {
        someRequesZ(requestYResponse.someValue).execute();    
    })
    .map(requestYResponse-> {
        return someRequesK(requestYResponse.someValue).execute();    
    })
    .map(requestKResponse -> {
        return someRequesJ(requestKResponse.someValue).execute();    
    })
    .subscribe(requestJResponse -> {
       doSOmethingWithFinalResponse(requestJResponse );
    })
Sign up to request clarification or add additional context in comments.

Comments

1

First of all, for network requests is better to use Single then Observable, because there always will be only one item. To switch from one requests to another, you can use flatMap.

Assuming your code is similar, you can try this:

class Dto1 {}

class Dto2 {}

class Dto3 {}

public interface Api {

    Single<Dto1> getDto1();

    Single<Dto2> getDto2();

    Single<Dto3> getDto3();
}

private Api api;

public void callApi() {
    api.getDto1()
            .doOnSuccess(dto1 -> {/*do something with dto1*/})
            .flatMap(dto1 -> api.getDto2())
            .doOnSuccess(dto2 -> {/*do something with dto2*/})
            .flatMap(dto2 -> api.getDto3())
            .doOnSuccess(dto3 -> {/*do something with dto3*/})
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe()
}

Comments

0

For the same scenario, i use concat operator which takes multiple Observables and concatenates their sequence

If response sequence doesn't require then you can use merge operator also.

Concat VS Merge operaror

Comments

0

try doOnNext() or map() method of Observable and use sync execute() of each response and pass them further

2 Comments

Can you please explain, why not use doOnNext() or map()? Because i have no further options without doOnNext() in Retrofit with Rx Java in Android e.g api.uploadImage(pref.getAuthToken(), screenshot) after that execute() is not showing in any options after dot operator.
the answer became rather big for the comment, I gave a full answer in thread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.