以特定格式向API发送数据时出错

时间:2017-02-07 09:40:37

标签: android api android-volley

我想以格式

的形式向服务器发送请求
[  
   {  
      "user_id":1,
      "is_estimate":1,
      "final_amount":100,
      "from_estimated_amount":0,
      "to_estimated_amount":0,
      "total_quantity":5,
      "quote_details":[  
         {  
            "quote_estimate_id":1,
            "job_reference_number":1,
            "species_id":1,
            "backing_option_id":1,
            "sheet_size_id":1,
            "substrate_id":1,
            "substrate_thickness_id":1,
            "edging_id":1,
            "species_quantity":1,
            "edging_quantity":1,
            "special_note":"test",
            "species_price":25,
            "sheet_size_price":15,
            "backing_price":15,
            "substrate_price":10,
            "is_prefinished":1,
            "prefinished_price":25,
            "edging_price":10,
            "wastage_percentage":4,
            "business_marging_min_percent":50,
            "business_marging_max_percent":50,
            "labour_price":50
         },
         {  
            "quote_estimate_id":1,
            "job_reference_number":1,
            "species_id":1,
            "backing_option_id":1,
            "sheet_size_id":1,
            "substrate_id":1,
            "substrate_thickness_id":1,
            "edging_id":1,
            "species_quantity":1,
            "edging_quantity":1,
            "special_note":"test",
            "species_price":25,
            "sheet_size_price":15,
            "backing_price":15,
            "substrate_price":10,
            "is_prefinished":1,
            "prefinished_price":25,
            "edging_price":10,
            "wastage_percentage":4,
            "business_marging_min_percent":500,
            "business_marging_max_percent":500,
            "labour_price":500
         }
      ]
   }
]

我该怎么做?

1 个答案:

答案 0 :(得分:1)

使用下面的代码构成你的Json,并使用POST / GET方法传递这个数组。

   JSONArray jArrayWhole = new JSONArray();
    JSONObject jObject = new JSONObject();
    jObject.put("user_id", "val");
    jObject.put("is_estimate", 1);
    jObject.put("final_amount", 100);
    jObject.put("from_estimated_amount", 0);
    jObject.put("to_estimated_amount", 0);
    jObject.put("total_quantity", 5);
    JSONArray jArrayDetail = new JSONArray();
    for (int i = 0; i < 2; i++) {
        JSONObject jObj = new JSONObject();
        jObject.put("quote_estimate_id", 1);
        jObject.put("job_reference_number", 1);
        jObject.put("species_id", 1);
        jObject.put("backing_option_id", 1);
        jObject.put("sheet_size_id", 1);
        jObject.put("substrate_id", 1);
        jObject.put("substrate_thickness_id", 1);
        jObject.put("edging_id", 1);
        jObject.put("species_quantity", 1);
        jObject.put("edging_quantity", 1);
        jObject.put("special_note", "test");
        jArrayDetail.put(jObj);
    }
    jArrayWhole.put(jObject);

如果使用GET方法,可以使用jArrayWhole.toString()。