I have an Arraylist private ArrayList<Product> listProducts = new ArrayList<>();
I just want to copy some elements (id and quantity) from this ArrayList and save it in a JSONArray so I can POST it.
My current code is this:
JSONObject obj = new JSONObject();
JSONArray cartitems = new JSONArray();
for (int i=0; i < listProducts.size(); i++) {
try {
obj.put("id", id);
obj.put("quantity", quantity);
cartitems.put(obj);
}catch (JSONException e) {
throw new RuntimeException(e);
}
}
After saving the JSONArray, I should POST it together with other values to PHP using Volley.
Here's my code:
CartRequest cartRequest = new CartRequest(total_amount, user_id, date, time, cartitems);
RequestQueue queue = Volley.newRequestQueue(ShoppingCartActivity.this);
queue.add(cartRequest);
Here's my Volley Request:
public class CartRequest extends StringRequest {
private static final String REQUEST_URL = "MY_URL";
private Map<String, String> params;
public CartRequest(String total_amount, String user_id, String date, String time, JSONArray cartitems){
super(Request.Method.POST, REQUEST_URL, null, null);
params = new HashMap<>();
params.put("total_amount", total_amount);
params.put("user_id", user_id);
params.put("date", date);
params.put("time", time);
params.put("cartitems", cartitems);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
But I'm getting an ERROR on my Volley Request:
Error:(25, 15) error: method put in interface Map<K,V> cannot be applied to given types;
required: String,String
found: String,JSONArray
reason: actual argument JSONArray cannot be converted to String by method invocation conversion
where K,V are type-variables:
K extends Object declared in interface Map
V extends Object declared in interface Map
Can somebody help me about this? I am new in Android and I am not sure if what I am doing wrong. Thanks in advance.