简体   繁体   中英

How to send arraylist of objects as parameter to a json array containing different fields using volley to a url in android?

在此处输入图片说明

The JSON array in postman containing product_id, unit_price and quantity shown in the picture.

I am getting a problem with posting ArrayList of objects as a parameter to properly populate the values against the respective fields mentioned in JSON array. Please anyone who can solve my problem? 在此处输入图片说明

Below is my java code: public void placeOrder(final ArrayList productArrayList) {

    btnPlaceOrder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String urlPlaceOrder = "http://radial-energy.com/radial/api/place-order";

            StringRequest stringRequest = new StringRequest(Request.Method.POST, urlPlaceOrder,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Toast.makeText(OrderDetails.this, "Response: " + response, Toast.LENGTH_SHORT).show();

                            JSONObject jsonObjectResponse;

                            try {
                                jsonObjectResponse = new JSONObject(response);

                                boolean status = jsonObjectResponse.getBoolean("success");

                                if (status) {

                            Toast.makeText(OrderDetails.this, "Order placed successfully!", Toast.LENGTH_SHORT).show();

                                    Intent placeOrderIntent = new Intent(OrderDetails.this,Home.class);
                                    startActivity(placeOrderIntent);

                                } else {
                                    Toast.makeText(OrderDetails.this, "Nothing!", Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                            Toast.makeText(OrderDetails.this, error.getMessage(),
                                    Toast.LENGTH_SHORT).show();
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {

                    Map<String, String> params = new HashMap<String, String>();

                    params.put("user_id", getUserId() + "");
                    params.put("distributor_id", 1 + "");

                    for (int i = 0; i < productArrayList.size(); i++) {
                        params.put("items", productArrayList.get(i).
                    }

                    params.put("total", totalPriceSum + "");
                    params.put("mechanic_id", 2 + "");

                    return params;
                }
            };

            Volley.newRequestQueue(OrderDetails.this).add(stringRequest);

        }
    });
}
JSONArray jsonArray = new JSONArray(productArrayList);
String jsonArrayString = jsonArray.toString();
params.put("items", jsonArrayString + "");

try convert array list to JSONArray than JSONArray to string and put it

You can convert list to Json using gson. Try as below:

@Override
protected Map<String, String> getParams() throws AuthFailureError {

  Map<String, String> params = new HashMap<String, String>();
  params.put("user_id", getUserId() + "");
  params.put("distributor_id", 1 + "");

  // Gson library does this for you
  params.put("items", new Gson().toJson(productArrayList)));

  params.put("total", totalPriceSum + "");
  params.put("mechanic_id", 2 + "");

  return params;
 }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM