0

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.

3
  • 1
    try this params.put("cartitems", String.valueOf(cartitems)); Commented Jul 20, 2016 at 11:35
  • or do params.put("cartitems", cartitems.toString()); Commented Jul 20, 2016 at 11:38
  • JSONObject obj; for (int i=0; i < listProducts.size(); i++) { obj = new JSONObject(); ...} Commented Jul 20, 2016 at 11:41

1 Answer 1

1

Replace it

JSONObject obj;
JSONArray cartitems = new JSONArray();
  for (int i=0; i < listProducts.size(); i++) {
     obj = new JSONObject();
     try {
         obj.put("id", id);
         obj.put("quantity", quantity);
         cartitems.put(obj);
     }catch (JSONException e) {
         throw new RuntimeException(e);
         }
     }

AND

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", String.valueOf(cartitems));
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, no ERROR anymore. But I'll have to check if my JSONArray is actually working.
put your jsonObject as new in for loop @AprilG
Yes I already did, I'm making my PHP file now to see if it's posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.