2017-02-27 83 views
0

我正在使用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拦截器做。

+0

使用字符串切割和附加是一个坏主意,以JSON。 – TruongHieu

+0

创建该数据的json对象并在请求中传递该对象。 –

+0

谢谢,我删除字符串附加并创建JSONObject。更新的问题。 –

回答

0

你让Json生病了!不要使用字符串剪切和追加来制作Json。 无论如何,如果你想知道什么是你做,你可以使用此代码下面

JsonObject json = JsonObject(params); 
Log.i("json", json.toString()); 
+0

不是答案.. –

+0

@CoDFather给出解释,CoD。他想知道他在体内发布什么,所以这是他发布的内容。 – TruongHieu

+0

你的回答不完整。它只会在你回答他的问题时完成。他想知道请求主体是否发送? –