Comment envoyer arraylist d'objets en paramètre à un tableau json contenant différents champs en utilisant volley à une URL dans android?
Le tableau JSON en facteur contenant product_id, unit_price et quantité indiquée dans le tableau.
Je rencontre un problème avec l'affichage de ArrayList d'objets en tant que paramètre pour remplir correctement les valeurs par rapport aux champs respectifs mentionnés dans le tableau JSON. S'il vous plaît quelqu'un qui peut résoudre mon problème?
Ci-dessous mon code java: PlaceOrder public void (finale 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);
}
});
}
Peut-être que vous pouvez l'envoyer en tant que chaîne? Hope [this] (https://stackoverflow.com/questions/42543111/how-to-send-jsonarray-to-server-in-android-using-stringentity) lien vous aide. –
Ça ne marche pas ... –
Pouvez-vous coller si vous obtenez une erreur? –