私は、WebサービスへのPOSTリクエストにVolley androidを使用しています。以下は私がボディに投稿したいフォーマットです。POSTリクエストで未処理のJSONObjectをバレーで渡す
// {
// "cust_id": "3",
// "amount": "150",
// "items": [
// {"itemid":"2",
// "qty":"4"},
// {"itemid":"5",
// "qty":"3"},
// {"itemid":"1",
// "qty":"5"}
// ]
// }
の項目(JSONArray)のリストは可変です。私は、私のコードが動作している
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);
のparams合格するには、以下の我々はOkHTTpインターセプタでそうであるように、私は完全なリクエストボディとヘッダをチェックしますかどのように私の質問を使用しています。
の下にこのコードを使用することができます。 – TruongHieu
そのデータのjsonオブジェクトを作成し、そのオブジェクトを要求に応じて渡します。 –
ありがとう、私は文字列の追加を削除し、JSONObjectを作成します。更新された質問。 –