我通过使用RxAndroid进行改造来调用api。这是方法的定义方式
@GET("product")
Observable<BaseResponse<List<Product>>> getProducts(@Query("lang") String lang,
@Query("category_id") String category_id,
@Query("start") String start,
@Query("count") String count);
此api返回Product
列表。我想处理列表中的每个Product对象,以检查该产品是否存在于本地db&amp;在产品对象中设置布尔值。所以我这样做了,
public Observable<List<Product>> getProductList(String catId, int start, int count) {
final List<Product> products = new ArrayList<>();
return RestClient.callApiWrapper(mContext, true, null,
RestClient.get().getProducts("en", catId, "" + start, "" + count))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.flatMapIterable(new Func1<BaseResponse<List<Product>>, Iterable<Product>>() {
@Override
public Iterable<Product> call(BaseResponse<List<Product>> listBaseResponse) {
Log.e("TAG", "flatMapIterable called");
return listBaseResponse.getData();
}
})
.map(new Func1<Product, List<Product>>() {
@Override
public List<Product> call(Product product) {
Log.e("TAG", "map called => " + products.size());
products.add(checkIfProductAddedToCart(product));
return products;
}
});
}
我订阅了getProductList
,我需要以这种方式处理已处理的列表:
mProductListDataModel.getProductList(mCategoryId, mStartOffset, mCount)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<Product>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(List<Product> products) {
Log.e("TAG", "onNext => " + products.size());
if(mOnProductListLoaded != null)
mOnProductListLoaded.onProductListLoaded(products);
}
});
现在问题是,API实际上返回了5个产品的列表,但onNext
每次被调用5次,大小增加到40个产品。请让我知道我在哪里做错了。
答案 0 :(得分:0)
我现在改变了一些东西,如下所示:
更改了getProductList
以返回Product
而不是List<Product>
所以它看起来像
public Observable<Product> getProductList(String catId, int start, int count) {
//final List<Product> products = new ArrayList<>();
return RestClient.callApiWrapper(mContext, true, null,
RestClient.get().getProducts("en", catId, "" + start, "" + count))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Func1<BaseResponse<List<Product>>, Observable<Product>>() {
@Override
public Observable<Product> call(BaseResponse<List<Product>> listBaseResponse) {
return Observable.from(listBaseResponse.getData());
}
})
.map(new Func1<Product, Product>() {
@Override
public Product call(Product product) {
//Log.e("TAG", "map called => " + products.size());
//products.add(checkIfProductAddedToCart(product));
//return products;
return checkIfProductAddedToCart(product);
}
});
}
并更改了此可观察者订阅的位置:
final List<Product> products = new ArrayList<>();
mProductListDataModel.getProductList(mCategoryId, mStartOffset, mCount)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Product>() {
@Override
public void onCompleted() {
Log.e("TAG", "onCompleted => " + products.size());
if (mOnProductListLoaded != null)
mOnProductListLoaded.onProductListLoaded(products);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Product product) {
products.add(product);
}
});
但是,我仍然不确定这是否是正确的做法。如果这不是正确的方法,请告诉我。