[英]Combine data from multiple API calls using retrofit and RxJava (Android)
我想做的是通過幾個不同的URL訪問數據,合並這些數據,然后在我的應用程序中使用它。 我可以輕松獲取所需的所有數據,但是我不知道如何將數據合並到一個列表中,並知道何時完成所有API調用(所有數據都在列表)。
這是我進行API調用的循環:
for(String diningCourt: diningCourts){
menuApi.getMenu(diningCourt, date)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(responseData -> {
//I want to add responseData to a list with
//the results from the rest of the calls
});
}
收集數據的正確方法是什么? 我嘗試使用靜態類變量來存儲結果,但最終只能獲得來自API調用之一的數據。 任何建議將不勝感激。
我們使用“ combineLatest”,請參見http://reactivex.io/documentation/operators/combinelatest.html。使用此運算符,兩個調用都將被執行,並且當兩個調用完成時,您將獲得結果。
Observable.combineLatest(observable1, observable2, (result1, result2) -> { //use both results })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.