Max
Max

Reputation: 679

Writing subsequent API requests with RxJava2 in Java

I need to write subsequent requests in my Android project. So first an API request is made and when the asynchronous response comes back, the response are used in the second request, and so on.

I've been studying the RxJava2 library but I haven't comprehended fully yet. Furthermore, the RxJava code will be in an Interactor class, that will call functions that reside in a repository, so I don't want to write out code directly inside the RxJava2 code, but call functions from another class. A GitHub repo that cover these areas would be very useful for me.

Upvotes: 0

Views: 76

Answers (1)

akarnokd
akarnokd

Reputation: 69997

The flatMap operator is a canonical way for specifying continuations that depend on the result(s) of a previous source(s):

retrofitAPI.getData(params)
.flatMap(data -> 
     retrofitAPI.getMoreData(data)
     .flatMap(moreData -> retrofitAPI.getEvenMoreData(data, moreData))
)

Upvotes: 3

Related Questions