【发布时间】:2018-05-03 16:23:02
【问题描述】:
我使用 RxAndroid 库 + Retrofit2。
我有 2 个发帖请求:
- 获取所有类别(返回列表 == 每个字符串都是类别 id)
- 按类别获取产品(返回列表)
我需要在启动应用程序后加载所有产品并保存到数据库。
当我创建 MainFragment 时,我得到所有类别:
restApiFactory.getProductService().getCategories(new CategoryRequest(initiatorId))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new CategoriesHandler());
并处理响应:
@Override
public void onNext(CategoryResponse value) {
List<CategoryItem> categoryItems = value.getCategoryItems();
...
}
然后我需要发送另一个请求(ProductsByCategory)但我不明白怎么做?
我可以在foreach中发送:
for (CategoryItem categoryItem : categoryItems) {
Observable<Products> product = ProductsByCategory...
}
或者可能有一些 Observable 合并 ....
我不知道。一般来说,如何做到这一点?对服务器的两个请求。一个将返回 id 列表和这些 id 上的第二个产品。
【问题讨论】:
标签: android retrofit2 rx-android