1

In my Android client I receive this JSON data from a backend:

[
    [
        1427378400000,
        553
    ],
    [
        1427382000000,
        553
    ]
]

Here is the routine which actually loads the data. I am using RxAndroid and Retrofit here.

private void getProductLevels() {
    Observable<List<List<Double>>> responseObservable =
        mProductService.readProductLevels();
    AppObservable.bindFragment(this, responseObservable)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        // TODO: Transform List<List<Double>> into List<ProductLevel>
        .subscribe(new Subscriber<List<List<Double>>>() {
            @Override
            public void onCompleted() {}

            @Override
            public void onError(Throwable e) {}

            @Override
            public void onNext(List<List<Double>> response) {}
        });

}

How can I map the inner List<Double> to a specific Java class such as ProductLevel using RxJava operators?

public class ProductLevel {

    public Double mTimeStamp;
    public Double mLevel;

    public ProductLevel(Double timeStamp, Double level) {
        mTimeStamp = timeStamp;
        mLevel = level;
    }

}

Finally, I would like to receive this: List<ProductLevel>.

4

3 回答 3

2

根据您的数据,您会收到一个配对列表(时间戳、级别)。这对由仅包含 2 个值的列表表示。

所以你想发出每一对,并将每一对转换成一个ProductLevel.

为此,您必须在flatMap您的配对列表中发出每一对。然后把map每一对变成一个ProductLevel。最后,只需构建一个list包含所有发射项目的项目。

(java8风格)

AppObservable.bindFragment(this, responseObservable)
             .subscribeOn(Schedulers.io())
             .observeOn(AndroidSchedulers.mainThread())
             .flatMapIterable(listOfList -> listOfList) // or flatMap(l -> Observable.from(l))
             .map(pair -> new ProductLevel(pair.get(0),pair.get(1))) // build ProductLevel for each pair
             .toList() // build a list with all ProductLevel
             .subscribe(listOfProductLevel -> /** ... **/);
于 2015-04-30T10:01:50.880 回答
0
List<ProductLevel> productLevels = new ArrayList<>();
for(List<Double> p: list) {
    productLevels.add(new ProductLevel(p.get(0),p.get(1)));
}

我认为它应该工作。

于 2015-04-29T15:31:52.517 回答
0

如果您更改readProductLevels为一次发出每个列表项,则可以一次对它们做出反应:

在 mProductService 中:

private Observable<List<Double>> readProductLevels() {
        return Observable.create(
                new Observable.OnSubscribe<List<Double>>() {
                    @Override
                    public void call(Subscriber<? super List<Double>> subscriber) {
                        List<List<Double>> items = new ArrayList<List<Double>>();
                        List<Double> list1 = new ArrayList<Double>() {{
                            add(1D);
                            add(2D);
                        }};
                        items.add(list1);
                        List<Double> list2 = new ArrayList<Double>() {{
                            add(10D);
                            add(12D);
                        }};
                        items.add(list2);
                        List<Double> list3 = new ArrayList<Double>() {{
                            add(21D);
                            add(22D);
                        }};
                        items.add(list3);

                       // Imagine the list creation above is your Json parsing


                        for (List<Double> list : items) {
                            subscriber.onNext(list);
                        }

                        subscriber.onCompleted();
                    }
                });
    }

允许您订阅超级列表中的每个项目:

private void getProductLevels() {
        Observable<List<Double>> responseObservable = readProductLevels();
        AppObservable.bindFragment(this, responseObservable)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                        // TODO: Transform List<List<Double>> into List<ProductLevel>
                .subscribe(
                        new Subscriber<List<Double>>() {
                            @Override
                            public void onCompleted() {
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onNext(List<Double> response) {
                                new ProductLevel(response.get(0), response.get(1));
                            }
                        });

    }
于 2015-04-29T15:58:16.157 回答