使用RxJava从两个API获取数据

我有两个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());
bieuosu 回答:使用RxJava从两个API获取数据

您需要的操作员列表:switchMap() fromIterable()toList()。因此您的代码将如下所示:

getCategoryProductsObservable()
        .subscribeOn(Schedulers.io())
        .switchMap { listOfProducts -> Observable.fromIterable(listOfProducts)
                    .map { it.sku }
                    .flatMap { sku -> getProductDetails(sku) }
                    .toList()
                    .map { listOfProductDetails -> /* combine with listOfProducts to Model you need */ }
        }
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(this::fillProducts);

我使用lambda来简化答案,可以重写为Function样式,但是我认为这是不言而喻的。

本文链接:https://www.f2er.com/2448029.html

大家都在问