JSONArray post 不适用于 Android 应用程序
JSONArray post not working from Android app
我试过使用 Postman,完全没问题
但是当我从应用程序 post 时,只有 table 和成本对象得到 posted。
这是我的 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": []
}
来自邮递员,
{
"_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"
}
]
}
此函数用于posting
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 时犯了什么错误?
您需要为 serve
创建一个 JSONArray
。为每个服务创建一个 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();
}
我试过使用 Postman,完全没问题
但是当我从应用程序 post 时,只有 table 和成本对象得到 posted。
这是我的 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": []
}
来自邮递员,
{
"_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"
}
]
}
此函数用于posting
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 时犯了什么错误?
您需要为 serve
创建一个 JSONArray
。为每个服务创建一个 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();
}