0

I am working on an application, where I had used Rx-android and Retrofit to do the network request, but now there is a requirement in the project where I have to do the nested network calls.I tried to google it out but didn't found any good article.If any one has worked on such topic then please let me know your findings.

Sam
  • 571
  • 1
  • 6
  • 22

2 Answers2

1

Assuming you're using retrofit with the rxjava adapter:

fun firstRequest(): Single<Response<String>>

fun secondRequest(idFromFirstRequest: String): Single<Response<ResponseBody>>

Use flatmap operator to chain the network calls:

firstRequest()
  // do more operators on the request, like transforming the object, or showing it first on the ui
  .flatMap { stringId -> secondRequest(stringId) }
  // you can flatMap here again to chain another network requests
  // .flatMap { thirdRequest() }
  // .flatMap { fourthRequest() }
  // and so on...
Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40
1

There are various articles related to API chaining, and the easiest way to achieve is using Rx-Java approaches

1) Using RxJava Zip operator (for parallel requests)

2) Using RxJava flatMap() operator(To request serially one after another)

Refer these two links for more detail examples

Synchronizing Network Calls With RxJava

Multiple api request using retrofit and rx java

Akshay Katariya
  • 1,464
  • 9
  • 20