简体   繁体   English

如何将对象的数组列表作为参数发送给包含不同字段的json数组,并使用Volley将其发送给android中的url?

[英]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. 如图所示,邮递员中的JSON数组包含product_id,unit_price和数量。

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. 我在发布对象的ArrayList作为参数以针对JSON数组中提到的各个字段正确填充值时遇到问题。 Please anyone who can solve my problem? 请任何可以解决我的问题的人吗? 在此处输入图片说明

Below is my java code: public void placeOrder(final ArrayList productArrayList) { 以下是我的Java代码: 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 尝试将数组列表转换为JSONArray而不是将JSONArray转换为字符串并放入

You can convert list to Json using gson. 您可以使用gson将列表转换为Json。 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;
 }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在android volley中将参数发送到json数组请求 - How to send params to json array request in android volley 在json中使用附加的json字符串迭代json数组作为url使用volley的响应 - Iterating through json array with appended json string in android after json as response from url using volley 如何反序列化包含不同类型对象的 json 数组 - How to deserialize a json array containing different types of objects 无法将包含嵌套对象的JSON数组转换为POJO arraylist - Unable to convert JSON array containing nested objects to POJO arraylist 如何使用以Json对象为参数的Volley发送删除请求? - How do i send Delete request using Volley with Json object as a parameter? 使用Android中的Volley发送JSON数组以将MySQL中的多个记录添加到PHP脚本中 - Send json array to add multiple records in mysql to php script using volley in android 如何在Android中使用Volley发送帖子请求? - How to send Post request using Volley in Android? 如何在 android 中使用 volley 获取 Json 对象中的 Json 数组,并在列表视图代码和 Xml Avilavble 中设置此 json 数据 - How to get Json Array in Json Object using volley in android and set this json data in listview code and Xml Avilavble 通过 volley android 将数组发送到 php - Send array by volley android to php 如何在包含不同对象的ArrayList中添加对象组的结尾 - How to add end of a object group in ArrayList containing different objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM