How to make multiple request and wait until data is come from all the requests in retrofit 2.0 - android

后端 未结 4 1244
南笙
南笙 2020-12-02 12:28

current code:

Retrofit retrofit = new Retrofit.Builder()
                  .baseUrl(Constant.BASEURL)
                  .addConverterFactory(GsonConverterFac         


        
4条回答
  •  温柔的废话
    2020-12-02 12:53

    for anybody checking this question. This works for me (Kotlin)

    fun manyRequestsNetworkCall(requests: ArrayList>, activity: Activity){
        Observable.zip(requests){results ->
            activity.runOnUiThread(Runnable {
               //do something with those results
               // runOnUiThread solves the problem cannot do something on background thread
            })
          // observeOn and subscribeOn solvesthe problem of NetworkOnMainThreadException
        }.observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .doOnSubscribe { userWorkdysResponse.value = Response.loading((requestType)) }
            .subscribe ({
               // do something when all the requests are done
            },{
               // do something if there is an error
            })
    }
    

提交回复
热议问题