如何发送嵌套的Jsonobject包含Jsonarray作为rest api的请求参数?

时间:2016-03-03 13:16:47

标签: android json android-volley

如何将这种类型的Jsonobject作为请求参数发送到android

中的rest API
{
    "operation": "diagnostic",
    "action": "DiagnosticCreateOrders",
    "diag_order_id": " 0",

    "data": [{
        "diag_odetail_id": "0",
        "diag_odetail_deptid": "100008",
        "diag_odetail_drname": "Dr Naren"
    }, {
        "diag_odetail_id": "0",
        "diag_odetail_deptid": "100008",
        "diag_odetail_drname": "Dr Naren"
    }]
}

我的代码是 com.android.volley.RequestQueue queue = Volley.newRequestQueue(this);

    StringRequest strReq = new StringRequest(Request.Method.POST,
           "http://172.19.1.57/API/v1/app/", new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {

            Log.d("ADDING_ORDER", "sending Server:---> " + response.toString());

            try {
                //JSONObject obj = new JSONObject(response);
                JSONObject obj = new JSONObject(response);
                Log.d("ADDING_ORDER", "Server Code---> " + obj.getString("code"));


                String code = obj.getString("code");


                if (code.equalsIgnoreCase("200")) {
                    Toast.makeText(getApplicationContext(), "Orderred Sucessfully Inserted", Toast.LENGTH_LONG).show();
                }
                if (code.equalsIgnoreCase("202")) {
                    Toast.makeText(getApplicationContext(), "Order Already assigned", Toast.LENGTH_LONG).show();

                }


            } catch (JSONException e) {

            }


        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            // Log.e("LOGIN", "Registration Error: " + error.getMessage());
            Log.d("LOGIN", "Login Response: Error Code" + error.toString());

        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to login url
            Map<String, String> mParams = new HashMap<String, String>();
            RequestParams params = new RequestParams();
            //mParams.put("Content-Type", "application/json");

            mParams.put("operation","diagnostic");
            mParams.put("action","DiagnosticCreateOrders");
            mParams.put("diag_order_id","0") ;

            List<Map<String, String>> listOfMaps = new ArrayList<Map<String,String>>();
            Map<String,String>  user1 = new HashMap<String, String>();
            user1.put("diag_odetail_id","0");
            user1.put(  "diag_odetail_deptid","100008");
            user1.put(  "diag_odetail_drname","Dr Naren");

            listOfMaps.add(user1);
           Map<String,String> user2 = new HashMap<String, String>();
            user2.put("diag_odetail_id","0");
            user2.put(  "diag_odetail_deptid","100005");
            user2.put(  "diag_odetail_drname","Dr Naren");


            listOfMaps.add(user2);
            mParams.put("data", listOfMaps.toString());
            Log.d("ADDING_ORDER_PARAMETERS", "parametrs:---> " + mParams.toString());

            return mParams;


        }

    };
    queue.add(strReq);*/

我使用了volley库,Requestparams和httpclient来发送此请求,但是我收到了这个错误......

{
    "status": {
        "type": "error",
        "code": "400",
        "disc": "Invalid JSON value found"
    },
    "request": "diag_city=Hyderabad&diag_order_type_e=1&diag_landmark=Road+Number+27%2C+Venkatagiri%2C+Jubilee+Hills%2C+Hyderabad%2C+Telangana+500033%2C+India&diag_patient_gender_e=1&diag_order_status_e=1&diag_delivery_state=Telangana&diag_address1=Road+Number+27&diag_address2=Venkatagiri&diag_address3=Jubilee+Hills&diag_patient_first_name=Srilaxmi&diag_contact_number=8768765432&action=DiagnosticCreateOrders&diag_patient_last_name=&diag_order_allstatus_e=1&diag_patient_mrn=203158&diag_delivery_locality=Hyderabad&diag_contact_mail=chpatient4%40gmail.com&diag_delivery_doorno=Road+Number+27&diag_cod=2&diag_patient_id=104170&diag_delivery_pincode=500033&source_type=Mobile+App&diag_handling_facility_id=100051&token=5b2d4293c67ac3c7cbbe8daf5e7a6882&diag_delivery_lat=17.439461299999998&diag_patient_age=34&diag_service_type_id=3&diag_order_facility_id=1&diag_business_source=3&diag_order_id=0&diag_delivery_lng=78.4079256&diag_country=India&operation=diagnostic&diag_country_code=91&diag_schedule_date=2016-02-23T20%3A45%3A00.000Z&diag_service_subtype_id=13&data=%5B%7B%22diag_odetail_id%22%3A%220%22%2C%22diag_odetail_deptid%22%3A%22100008%22%2C%22diag_odetail_drname%22%3A%22Dr+Naren%22%2C%22diag_odetail_facility_need%22%3A%220%22%2C%22diag_odetail_facilitation_require%22%3A%221%22%2C%22diag_odetail_gross_amount%22%3A%22250.000000%22%2C%22diag_odetail_net_amount%22%3A%22250.000000%22%2C%22diag_odetail_service_id%22%3A%22100989%22%2C%22diag_odetail_status_e%22%3A%221%22%2C%22diag_odetail_order_qty%22%3A%221%22%2C%22diag_odetail_patientid%22%3A%22104170%22%2C%22diag_odetail_prescribed%22%3A%221%22%2C%22diag_odetail_priority_e%22%3A%225%22%2C%22diag_odetail_schedule_date%22%3A%222016-02-23T20%3A45%3A00.000Z%22%2C%22diag_odetail_subdept_id%22%3A%22100008%22%2C%22diag_odetail_testid%22%3A%22100989%22%2C%22diag_odetail_testname%22%3A%22Complete+Blood+Count+%28CBC%29+-+Whole+Blood%22%2C%22diag_odetail_testprice%22%3A%22250.000000%22%2C%22diag_odetail_testtype_e%22%3A%222%22%2C%22diag_odetail_apostatus_e%22%3A%221%22%2C%22diag_odetail_order_guid%22%3A%22r1r2f3ffff4%22%7D%2C%7B%22diag_odetail_id%22%3A%220%22%2C%22diag_odetail_deptid%22%3A%22100005%22%2C%22diag_odetail_drname%22%3A%22Dr+Naren%22%2C%22diag_odetail_facility_need%22%3A%220%22%2C%22diag_odetail_facilitation_require%22%3A%221%22%2C%22diag_odetail_gross_amount%22%3A%22113.000000%22%2C%22diag_odetail_net_amount%22%3A%22113.000000%22%2C%22diag_odetail_service_id%22%3A%22101043%22%2C%22diag_odetail_status_e%22%3A%221%22%2C%22diag_odetail_order_qty%22%3A%221%22%2C%22diag_odetail_patientid%22%3A%22104170%22%2C%22diag_odetail_prescribed%22%3A%221%22%2C%22diag_odetail_priority_e%22%3A%225%22%2C%22diag_odetail_schedule_date%22%3A%222016-02-23T20%3A45%3A00.000Z%22%2C%22diag_odetail_subdept_id%22%3A%22100005%22%2C%22diag_odetail_testid%22%3A%22101043%22%2C%22diag_odetail_testname%22%3A%22Fasting+Blood+Glucose+%28FBS%29+-+Plasma%22%2C%22diag_odetail_testprice%22%3A%22113.000000%22%2C%22diag_odetail_testtype_e%22%3A%222%22%2C%22diag_odetail_apostatus_e%22%3A%221%22%2C%22diag_odetail_order_guid%22%3A%22r1r2f3ffff4%22%7D%5D"
}

请帮帮我怎么发送......

1 个答案:

答案 0 :(得分:0)

最后,将jsonParam对象发送到您的服务

coords = pd.read_csv('IDQQCoords.csv') 

coords = coords.as_matrix(columns=None) 

mpty = np.zeros((len(coords),8),dtype=float) 
#creates an empty array the same length as coords

coords = np.append(coords,mpty,axis=1) 
# adds the 8 empty columns from the previous command
#This is to make space to add the values from subsequent rows 



cnt = 0
lth = coords.shape[0]
for counter in range(1,lth):

    if coords[cnt+1,0] == coords[cnt,0]:
        coords[cnt,3:5] = coords[cnt+1,1:3]        
        coords = np.delete(coords,cnt+1,axis=0)

    if coords[cnt+1,0] == coords[cnt,0]:
        coords[cnt,5:7] = coords[cnt+1,1:3]       
        coords = np.delete(coords,cnt+1,axis=0)

    if coords[cnt+1,0] == coords[cnt,0]:
        coords[cnt,7:9] = coords[cnt+1,1:3]
        coords = np.delete(coords,cnt+1,axis=0)

    if coords[cnt+1,0] == coords[cnt,0]:
        coords[cnt,9:11] = coords[cnt+1,1:3]        
        coords = np.delete(coords,cnt+1,axis=0)

    cnt = cnt+1