Rxjava网络嵌套

public void lastRequest(final HttpListener mHttpListener) {
    Map map = new HashMap<>();
    map.put("phone", "13568574500");
    map.put("pwd", "123456");
    final Observable post = this.httpService.post("small/user/v1/login", map);
    post.subscribeOn(Schedulers.io())
            .observeOn(Schedulers.io())
            .doOnNext(new Consumer() {
                @Override
                public void accept(ResponseBody responseBody) {
                    try {
                        String string = responseBody.string();
                        Gson gson = new Gson();
                        LoginBean loginBean = gson.fromJson(string, LoginBean.class);
                        headMap = new HashMap<>();
                        headMap.put("userId", loginBean.getResult().getUserId() + "");
                        headMap.put("sessionId", loginBean.getResult().getSessionId());
                        JSONObject object = new JSONObject();
                        object.put("commodityId", 5);
                        object.put("count", 156);
                        JSONArray array = new JSONArray();
                        array.put(object);
                        Log.e("jsonStri", array.toString());
                        put = HttpUtils.this.httpService.put("small/order/verify/v1/syncShoppingCart", headMap, array.toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).subscribeOn(Schedulers.io())
            .flatMap(new Function>() {
                @Override
                public ObservableSource apply(ResponseBody responseBody) {
                    return put;
                }
            }).doOnNext(new Consumer() {
        @Override
        public void accept(ResponseBody responseBody) {

            try {
                Gson gson = new Gson();
                JoinShopping joinShopping = gson.fromJson(responseBody.string(), JoinShopping.class);
                if (joinShopping.getStatus().equals("0000")) {
                    Log.e("加入购物车成功", "----");
                    shopping = HttpUtils.this.httpService.get("small/order/verify/v1/findShoppingCart", headMap);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).flatMap(new Function>() {
        @Override
        public ObservableSource apply(ResponseBody responseBody) {
            return shopping;
        }
    }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(ResponseBody responseBody) {
                    try {
                        mHttpListener.success(responseBody.string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

}

你可能感兴趣的:(Rxjava网络嵌套)