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.

4 Answers4

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 );
    })
Wackaloon
  • 2,285
  • 1
  • 16
  • 33
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()
}
Aleksander Mielczarek
  • 2,787
  • 1
  • 24
  • 43
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

0

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

Wackaloon
  • 2,285
  • 1
  • 16
  • 33
  • 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. – Ankush Goyal Sep 18 '17 at 16:42
  • the answer became rather big for the comment, I gave a full answer in thread – Wackaloon Sep 19 '17 at 19:33