我正在使用Volley android向我的webservice发送POST请求。以下是我想要在Body中发布的格式。在Volley中传递POST请求中的原始JSONObject
// {
// "cust_id": "3",
// "amount": "150",
// "items": [
// {"itemid":"2",
// "qty":"4"},
// {"itemid":"5",
// "qty":"3"},
// {"itemid":"1",
// "qty":"5"}
// ]
// }
items(JSONArray)list is variable。我使用下面通过PARAMS
JSONArray jsonArray = new JSONArray();
for (MenuItem m:orderedList) {
JSONObject obj1 = new JSONObject();
obj1.put("itemid", m.getImgid());
obj1.put("qty", m.getQty());
jsonArray.put(obj1);
}
JSONObject obj2 = new JSONObject();
obj2.put("cust_id",cust_id);
obj2.put("amount",totamt);
obj2.put("items",jsonArray);
Log.d("Volley request order",obj2.toString());
JsonObjectRequest req = new JsonObjectRequest(Config.ORDER_URL, obj2, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("Response is:%n %s", response.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error is: ", error.getMessage());
}
});
// Adding request to request queue
LifeCycle.getInstance().addToRequestQueue(req, tag_json_obj);
我的代码工作,我的问题我怎么检查完成请求正文和标题,因为我们在OkHTTp拦截器做。
使用字符串切割和附加是一个坏主意,以JSON。 – TruongHieu
创建该数据的json对象并在请求中传递该对象。 –
谢谢,我删除字符串附加并创建JSONObject。更新的问题。 –