我有两个api请求
//以获得特定类别内的所有产品列表
1. getcategoryProducts : Observable<List<CategoryProductsModel>
class CategoryProductsModel {
int categoryId ;
String sku ;
// sku is variable I used it to get the details for each product (name,images,colors,sizes,price ..etc
}
//根据第一个api getcategoryProducts通过sku获取产品详细信息
2.getProductDetails(var sku:String): Observable<ProductDetailsModel>
class ProductDetailsModel {
float price ;
List<Images> images,List<Colors> colors,etc ...
}
第一个请求是getcategoryProducts
,然后是getProductDetails
,基于第一个请求,以获取每个产品的sku
并将其传递给getProductDetails
发出第二个请求后,我想在ProductModel
中添加数据响应,以最终获得ProductModel
的列表并将其传递给recycler view
ProductModel
{
CategoryProductsModel mCategoryProductsModel ;
ProductDetailsModel mProductDetailsModel ;
}
我使用RxJava请求API。我该怎么办?
public void getProductsList() {
getcategoryProductsobservable()
.subscribeon(Schedulers.io())
.flatMap(new Function<CategoryProductsModel,ObservableSource<List<ProductModel>>>() {
@Override
public ObservableSource<List<ProductModel>> apply(CategoryProductsModel mCategoryProductsModel) throws Exception {
return getProductDetails(mCategoryProductsModel);
}
}).observeon(AndroidSchedulers.mainThread())
.subscribe(this::fillProducts);
}
public Observable<CategoryProductsModel> getcategoryProductsobservable() {
return Repository.Companion.getStoreInstance().getcategoryProducts(categoryId).subscribeon(Schedulers.io())
.observeon(AndroidSchedulers.mainThread())
.flatMap(new Function<List<CategoryProductsModel>,ObservableSource<CategoryProductsModel>>() {
@Override
public ObservableSource<CategoryProductsModel> apply(List<CategoryProductsModel> mCategoryProductsList) throws Exception {
return Observable.fromIterable(mCategoryProductsList).subscribeon(Schedulers.io());
}
}).subscribeon(Schedulers.io());
}
@SuppressLint("CheckResult")
public Observable<List<ProductModel>> getProductDetails(CategoryProductsModel mCategoryProducts) {
List<ProductModel> mList = new ArrayList<>() ;
return Repository.Companion.getStoreInstance().getProductDetails(mCategoryProducts.getSku())
.map(new Function<ProductDetailsModel,List<ProductModel>>() {
@Override
public List<ProductModel> apply(ProductDetailsModel mProductDetailsModel) throws Exception {
Log.d("mProductDetailsModel",mProductDetailsModel.getName());
ProductModel productModel = new ProductModel() ;
productModel.setProductDetailsModel(mProductDetailsModel);
productModel.setCategoryProductsModel(mCategoryProducts);
mList.add(productModel);
return mList;
}
}).subscribeon(Schedulers.io());