retrofit flatmap和map

public class Test {
    private Map map = new HashMap<>();
    private int index = 0;

    public void test() {
        List videoTests = new ArrayList<>();

        for (int i = 0; i < 6; i++) {
            TaskInfo videoTest = new TaskInfo();
            videoTest.setParams("s" + i + "s");
            videoTests.add(videoTest);
        }
        Observable.fromArray(videoTests)//这里模拟请求数据集合
                .flatMap(new Function, ObservableSource>() {
                    @Override
                    public ObservableSource apply(List pVideoTests) {
                        index = 0;
                        return Observable.fromIterable(pVideoTests);// 这里的fromIterable是一个一个发送数据
                    }
                })
                //单独处理每个数据
                .map(new Function() {
                    @Override
                    public String apply(TaskInfo pVideoTest) {
                        //这里需要使用map来绑定对象跟key key可以使用integer
                        map.put(index, pVideoTest);
                        return pVideoTest.getParams();
                    }
                })
                .map(new Function() {
                    @Override
                    public String apply(String ps) {
                        //这边处理url 应该是请求接口B 我这就简单点处理了
                        return ps + "处理过的";
                    }
                })
                .map(new Function() {
                    @Override
                    public TaskInfo apply(String pS) {
                        //这边应该将正确的url赋值给对应的对象
                        TaskInfo videoTest = map.get(index);
                        videoTest.setParams(pS);
                        index++;
                        return videoTest;
                    }
                })
                //这个操作符是用来收集对象的
                .collect(new Callable>() {
                    @Override
                    public List call() {
                        return new ArrayList<>();
                    }
                }, new BiConsumer, TaskInfo>() {
                    @Override
                    public void accept(List pNewses, TaskInfo pNews) {
                        pNewses.add(pNews);
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer>() {
                    @Override
                    public void accept(List pVideoTests) {
                        for (TaskInfo v : pVideoTests) {
                            Log.i("WY", "---->" + v.getParams());
                        }
                    }
                }, new Consumer() {
                    @Override
                    public void accept(Throwable pThrowable) {
                        Log.e("WY", pThrowable.getMessage());
                    }
                });

    }
}

你可能感兴趣的:(学习)