【问题标题】:Get Data from Two api using RxJava使用 RxJava 从两个 api 获取数据
【发布时间】:2020-07-26 18:28:00
【问题描述】:

我有两个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());

【问题讨论】:

    标签: android api retrofit rx-java flatmap


    【解决方案1】:

    您需要的运算符列表: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);
    

    我使用 lambdas 来获得更短的答案,可以重写为 Function 样式,但我想这是不言自明的。

    【讨论】:

    • 谢谢,你能给我一个完整的代码,我想得到 ProductModel 的列表吗?
    • @MonisAttili 模型 ProductDetailsModel 是否包含 String sku 归档?
    • 是的,它有 sku,productDetailModel 是来自 magento 的 api 响应,用于获取产品的详细信息。
    • @GET("categories/{category_id}/products") fun getCategoryProducts(@Path("category_id") categoryId:Int): Observable> @GET("products/{ sku}") fun getProductDetails(@Path("sku") sku:String): Observable
    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2018-11-25
    • 1970-01-01
    相关资源
    最近更新 更多