为什么我的名单在排球时是空的?

时间:2018-01-30 10:05:23

标签: java android json spring-boot arraylist

  

我现在正试图解决这个问题!我想发帖   从android到的数据   Java使用rest web services我   创建

     

android中,我   创建了一个私有方法来获取所有产品并准备好它们   对于http post,因此列表中每个项目的映射:

private List getAllProducts() {

        ArrayList formatProducts = new ArrayList<>();

        for ( int i =0; i < ShoppingCartController.getInstance().myProducts.size(); i++ ) {

            Item allProducts = ShoppingCartController.getInstance().myProducts.get(i);

            Map<Object, Object> insertData = new HashMap<>();

            formatProducts.add( new SaveOrderFormat(
                    insertData.put("name", allProducts.getName() ),
                    insertData.put("manufacture", allProducts.getManufacture() ),
                    insertData.put("price", allProducts.getPrice() ),
                    insertData.put("image", allProducts.getImage() ),
                    insertData.put("count", allProducts.getQuantity() ) )
            );
        }

        return formatProducts;
    }
  

我的volleyCall方法:

private void volleyCall(String email, int totalQuantity, double cost, String phoneNumber, String address, String closeShop,
                            String contact, String orderDate, String deliveryDate, List orderedItems ) {

        RequestQueue queue = Volley.newRequestQueue(this);
        String URL = "http://........:8080/order/service/save/order";
        Map<String, Object> jsonParams = new HashMap<>();
        jsonParams.put("cusEmail", email);
        jsonParams.put("totalQuantity", totalQuantity);
        jsonParams.put("totalCost", cost);
        jsonParams.put("phoneNumber", phoneNumber);
        jsonParams.put("address", address);
        jsonParams.put("closeShop", closeShop);
        jsonParams.put("contact", contact);
        jsonParams.put("status", "ordered");
        jsonParams.put("orderDate", orderDate);
        jsonParams.put("deliveryDate", deliveryDate);
        jsonParams.put("orderedItems", orderedItems );

        Log.d( TAG, "Json:" + new JSONObject(jsonParams));
        JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, URL, new JSONObject(jsonParams),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // handle error
                        Log.d(TAG, "Error" + error + "\nmessage" + error.getMessage() );
                    }
                });

        queue.add(postRequest);
    }
  

OnCreate方法调用volley:

volleyCall(
        ShoppingCartController.getInstance().keepUserSession, ShoppingCartController.getInstance().totalQuantity, 
        ShoppingCartController.getInstance().totalCart, str_phoneNumber, str_address, str_close, 
        contact, dateFormat.format(orderDate),
        String.valueOf(dateFormat.format(new Date(deliverYear - 1900, deliverMonth, deliverDay))),
        getAllProducts() 
);

Log.d("*******: : : : ", getAllProducts().toString() );
  

在我的控制台日志(android studio)中,我有以下声明:

D/Saving Order : : :: Json:{"cusEmail":null,"totalQuantity":2,"phoneNumber":"","address":"","closeShop":"","contact":"By email:::","orderedItems":[null,null],"deliveryDate":"0002-12-31","orderDate":"2018-01-30","totalCost":29.48,"status":"ordered"}

D/*******: : : :: [store.pnp.com.pnp_store.helper.SaveOrderFormat@8a88345, store.pnp.com.pnp_store.helper.SaveOrderFormat@41d1a9a]

D/Saving Order : : :: Errorcom.android.volley.ParseError: org.json.JSONException: End of input at character 0 of 
                      messageorg.json.JSONException: End of input at character 0 of 
  

我的问题是,我的arrayList返回的json数据   发布数据时,控制台上的“orderedItems”:[null,null]   然后导致保存顺序错误(字符0处的输入结束)。虽然不太确定错误!

如何解决此错误?

顺便说一句,发布和检索时,我的json数据应如下所示:

[
    {
        "id": 2,
        "cusEmail": "...",
        "totalQuantity": 11,
        "totalCost": 296.89,
        "phoneNumber": "844 463 9211",
        "address": "...",
        "closeShop": "...",
        "contact": "...",
        "status": "...",
        "orderDate": "2017-15-12",
        "deliveryDate": "2017-12-18",
        "orderedItems": [
            {
                "id": 1,
                "name": "...",
                "manufacture": "...",
                "price": 13.99,
                "image": "data:image/jpeg;base64,/...
                "count": 3
            },
            {
                "id": 2,
                "name": "...",
                "manufacture": "...",
                "price": 20.99,
                "image": "data:image/jpeg;base64,/...
                "count": 7
            },
            {..., ..., ...}
        ]
    }
]

2 个答案:

答案 0 :(得分:0)

        Map<Object, Object> insertData = new HashMap<>();

        formatProducts.add( new SaveOrderFormat(
                insertData.put("name", allProducts.getName() ),
                insertData.put("manufacture", allProducts.getManufacture() ),
                insertData.put("price", allProducts.getPrice() ),
                insertData.put("image", allProducts.getImage() ),
                insertData.put("count", allProducts.getQuantity() ) )
        );

您在这里丢弃所有产品数据。您将其添加到insertData,但从不使用insertData。您添加到SaveOrderFormat的内容只是一堆null s。

关于put的javadoc:

  

返回:   与key关联的上一个值,如果没有key的映射,则返回null。

答案 1 :(得分:0)

问题在这里

        jsonParams.put("orderedItems", orderedItems );

你必须放的是地图,而不是列表。 例如:

 Map<Object, Object> insertData = new HashMap<>();

    formatProducts.add( new SaveOrderFormat(
            insertData.put("name", allProducts.getName() ),
            insertData.put("manufacture", allProducts.getManufacture() ),
            insertData.put("price", allProducts.getPrice() ),
            insertData.put("image", allProducts.getImage() ),
            insertData.put("count", allProducts.getQuantity() ) )
    );

 jsonParams.put("orderedItems", insertData);