在我的 Android 客户端中,我从后端收到以下 JSON 数据:
[
[
1427378400000,
553
],
[
1427382000000,
553
]
]
这是实际加载数据的例程。我在用接收Android https://github.com/ReactiveX/RxAndroid and Retrofit http://square.github.io/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) {}
});
}
如何绘制内部图List<Double>
特定的 Java 类,例如ProductLevel
using RxJava 运算符?
public class ProductLevel {
public Double mTimeStamp;
public Double mLevel;
public ProductLevel(Double timeStamp, Double level) {
mTimeStamp = timeStamp;
mLevel = level;
}
}
最后,我希望收到这样的信息:List<ProductLevel>
.
根据您的数据,您会收到一个对(时间戳、级别)的列表。该对由仅包含 2 个值的列表表示。
所以你想要emit每对,以及转换每对分成一个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 -> /** ... **/);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)