title: Doing multiple RxJava calls and using result for next call
body: <p>I'm trying to use RxJava to solve this problem. I have 3 calls that need to be executed after each other, using the result of the last call for the next call.</p>
  
  <p>For sake of simplicity I've hosted 3 files on my server that are the 3 calls:</p>
  
  <p><a href="http://jimclermonts.nl/booky/step1user" rel="nofollow noreferrer">http://jimclermonts.nl/booky/step1user</a></p>
  
  <p><a href="http://jimclermonts.nl/booky/step2cookie" rel="nofollow noreferrer">http://jimclermonts.nl/booky/step2cookie</a></p>
  
  <p><a href="http://jimclermonts.nl/booky/step3token" rel="nofollow noreferrer">http://jimclermonts.nl/booky/step3token</a></p>
  
  <p>What is the correct and cleanest way to do this?</p>
  
  <p><strong>build.gradle:</strong></p>
  
  <pre><code>// Retrofit
  implementation 'com.squareup.retrofit2:retrofit:2.3.0'
  implementation 'com.squareup.retrofit2:converter-moshi:2.3.0'
  implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
  implementation 'com.squareup.okhttp3:okhttp:3.9.0'
  
  // reactive extensions
  implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
  implementation 'io.reactivex.rxjava2:rxjava:2.1.7'
  implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
  </code></pre>
  
  <p><strong>MainActivity:</strong></p>
  
  <pre><code>override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
  
      val result = SomeWebService().getToken()
      if (result != null) {
          result.doOnComplete { 
              //how to get the token value?
          }
      }
  }
  </code></pre>
  
  <p><strong>SomeWebService</strong></p>
  
  <pre><code>class SomeWebService {
  
      lateinit var apiService: ApiService
      var tokenResult: Observable&lt;String&gt;? = null
  
      fun getToken() : Observable&lt;String&gt;? {
          if (tokenResult == null) {
              apiService = ApiService.retrofit.create(ApiService::class.java)
              val body = step1ApiUserResponse()
              val cookie = step2CookieResponse(body.blockingSingle())
              val tokenResult = step3TokenResponse(cookie.blockingSingle())
              this.tokenResult = tokenResult
              tokenResult.doOnComplete {  }
      } else {
          tokenResult!!.doOnComplete {  }
      }
      return tokenResult
      }
  
      fun step1ApiUserResponse(): Observable&lt;String&gt; {
          return Observable.create {
              apiService.getStep1User()
                      .subscribeOn(Schedulers.io())
                      .observeOn(AndroidSchedulers.mainThread())
                      .doOnSuccess { user -&gt;
                          val body = user.getResponse()
                          if (body != null) {
                              it.onNext(body)
                          }
                      }
                      .doOnError {
                          it.printStackTrace()
                      }
          }
      }
  
      fun step2CookieResponse(body: String): Observable&lt;String&gt; {
          return Observable.create {
              apiService.getStep2Cookie(body)
                      .subscribeOn(Schedulers.io())
                      .observeOn(AndroidSchedulers.mainThread())
                      .doOnSuccess { result -&gt;
                          val bodyResult = result.body().toString()
                          it.onNext(bodyResult)
                      }
          }
      }
  
      fun step3TokenResponse(cookie: String): Observable&lt;String&gt; {
          return Observable.create {
              apiService.getStep3Token(cookie)
                      .subscribeOn(Schedulers.io())
                      .observeOn(AndroidSchedulers.mainThread())
                      .doOnSuccess { result -&gt;
                          val body = result.getToken()
                          if (body != null) {
                              it.onNext(body)
                          }
                      }
          }
      }
  }
  </code></pre>
  
  <p><strong>ApiService:</strong></p>
  
  <pre><code>interface ApiService {
  
      @GET("/booky/step1user")
      fun getStep1User(): Single&lt;UserResponse&gt;
  
      @GET("/booky/step2cookie")
      fun getStep2Cookie(body: String): Single&lt;Response&gt;
  
      @GET("/booky/step3token")
      fun getStep3Token(@Header("Cookie") sessionId: String): Single&lt;TokenResponse&gt;
  
      companion object {
  
          val interceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
          val client = OkHttpClient.Builder()
                  .addInterceptor(interceptor)
                  .build()
  
          val retrofit = Retrofit.Builder()
                  .baseUrl("http://jimclermonts.nl")
                  .addConverterFactory(MoshiConverterFactory.create().asLenient())
                  .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                  .client(client)
                  .build()
      }
  }
  </code></pre>
  
  <p><strong>TokenResponse:</strong></p>
  
  <pre><code>class TokenResponse {
  
      @Json(name = "Token")
      private var token: String? = null
  
      fun getToken(): String? {
          return token
      }
  
      fun setToken(token: String) {
          this.token = token
      }
  }
  </code></pre>
  
  <p><strong>UserResponse:</strong></p>
  
  <pre><code>class UserResponse {
  
      @Json(name = "Response")
      private var response: String? = null
  
      fun getResponse(): String? {
          return response
      }
  
      fun setResponse(response: String) {
          this.response = response
      }
  }
  </code></pre>