JSONArray帖子无法在Android应用中运行

时间:2016-04-14 16:21:20

标签: android post android-volley jsonobject

我尝试使用Postman,它完全正常

Postman Screenshot

但是当我从应用程序发布时,只会发布表和成本对象。

这是我的Android代码

JSONObject sendObj = null;

            sendObj = new JSONObject();
            try {
                sendObj.put("table",mTableUser);
                sendObj.put("cost",cost);

                for(int i=0;i<mOrderFoodList.size();i++) {

                    sendObj.put("serve["+i+"][food]",mOrderFoodList.get(i).getID() );
                    sendObj.put("serve["+i+"][number]",mOrderFoodList.get(i).getQuantity() );
                    sendObj.put("serve["+i+"][status]", 0);

                }


            }catch (JSONException e) {
                e.printStackTrace();
            }
 mVolleyService.postDataVolley("POSTCALL", mConstants.BASE_URL +mConstants.ORDER, sendObj);

这是Android App的输出

  {
"_id": "570fc02422ed0203002723d2",
"cost": 60,
"table": 1,
"__v": 0,
"serve": []
}

来自Postman,

 {
"_id": "570faed422ed0203002723c8",
"cost": 200,
"table": 20,
"__v": 0,
"serve": [
  {
    "food": "56fac73f284ea80300ba6b42",
    "quan": 4,
    "status": 0,
    "_id": "570faed422ed0203002723c9"
  },
  {
    "food": "56fac6d0284ea80300ba6b41",
    "quan": 3,
    "status": 0,
    "_id": "570faed422ed0203002723ca"
  }
]
}

此功能用于发布

 public void postDataVolley(final String requestType, String url,JSONObject sendObj){
    try {
         queue = Volley.newRequestQueue(mContext);

        JsonObjectRequest jsonObj = new JsonObjectRequest(url,sendObj, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if(mResultCallback != null)
                    mResultCallback.notifySuccess(requestType,response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if(mResultCallback != null)
                    mResultCallback.notifyError(requestType,error);
            }

        })
        {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put("x-access-token", token);


                return headers;
            }



        };

        queue.add(jsonObj);

    }catch(Exception e){

    }
}

我在发送JSONObject时犯了什么错误?

1 个答案:

答案 0 :(得分:0)

您需要为JSONArray创建serve。为每个服务创建JSONObject并将其放入JSONArray

试试这段代码,

JSONObject sendObj = new JSONObject();
try {
    sendObj.put("table",mTableUser);
    sendObj.put("cost",cost);

    JSONArray serveArray = new JSONArray();
    for(int i=0; i<mOrderFoodList.size(); i++) {
        JSONObject singleServe = new JSONObject();
        singleServe.put("food",mOrderFoodList.get(i).getID() );
        singleServe.put("number",mOrderFoodList.get(i).getQuantity() );
        singleServe.put("status", 0);
        serveArray.putJSONObject(singleServe);
    }
    sendObj.putJSONArray(serveArray);

} catch (JSONException e) {
    e.printStackTrace();
}