0

I want to pass array of objects using my retrofit request. through Volley we are passing the request array as,

{OrderId=11692, ItemsList[1][Amount]=1, ItemsList[1][id]=29, ItemsList[1][Notes]=2.0, ItemsList[0][TypeId]=23, ItemsList[0][Notes]=15.0, ItemsList[1][serviceId]=28, ItemsList[0][serviceId]=27, ItemsList[0][Amount]=3, ItemsList[0][id]=29, ItemsList[1][TypeId]=23}

How I can send a request using this format?

I am trying like this:

@FormUrlEncoded
@POST("api/shidhin/UpdateOrder")
Call<CommonResponse> updateOrderItems(@Header("Authorization") String token, @Field("OrderId") int orderid, @Field("ItemsList[]") ItemsList[] itemsList);

ItemsList.java class file

public class ItemsList {
    @SerializedName("id")
    private int id;
    @SerializedName("Amount")
    private int Amount;
    @SerializedName("Notes")
    private String Notes;
    @SerializedName("serviceId")
    private int serviceId;
    @SerializedName("TypeId")
    private int TypeId;
}
riza milani
  • 163
  • 1
  • 7
SHIDHIN TS
  • 1,557
  • 3
  • 26
  • 58

2 Answers2

2

I solved this issue by passing the object array as @FieldMap . Her is the way O solved this issue,

Create Object Array,

        Order.Orderitems orderitems;
        Map<String, String> parms = new HashMap<String, String>();
        for (int i = 0; i < orderItems.size(); i++) {
            orderitems = orderItems.get(i);
            parms.put("ItemsList[" + i + "][id]", String.valueOf(orderitems.getId()));
            parms.put("ItemsList[" + i + "][Amount]", String.valueOf(orderitems.getAmount()));
            parms.put("ItemsList[" + i + "][Notes]", "");
            parms.put("ItemsList[" + i + "][serviceId]", String.valueOf(orderitems.getServiceId()));
            parms.put("ItemsList[" + i + "][TypeId]", String.valueOf(orderitems.getServiceTypeId()));
        }

Also Made the API call Like,

@FormUrlEncoded
@POST("api/shidhin/UpdateOrder")
Call<CommonResponse> updateOrderItems(@Header("Authorization") String token, @Field("OrderId") int orderid, @FieldMap Map<String, String> parms);
SHIDHIN TS
  • 1,557
  • 3
  • 26
  • 58
0

If you want to upload array of object using Retrofit then follow the steps. It will work 100%. In my case I have 2 params one is userid and second is location_data. In second params I have to pass array of objects.

@FormUrlEncoded
@POST("api/send_array_data")
Call<StartResponseModal> postData(@Field("user_id") String user_id,
                                             @Field("location_data") String jsonObject);

then in your MainActivity.class.

 ArrayList<JSONObject> obj_arr;  // define this at top level.
 try {
                JSONArray jsonArray = new JSONArray();
                obj_arr = new ArrayList<>();
                 // LocationData is model class. 
                for (LocationData cart : arrayList) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("latitude", cart.getLatitude());
                    jsonObject.put("longitude", cart.getLongitude());
                    jsonObject.put("address", cart.getAddress());
                    jsonObject.put("battery", cart.getBattery());
                    jsonObject.put("is_gps_on", cart.getIs_gps_on());
                    jsonObject.put("is_internet_on", cart.getIs_internet_on());
                    jsonObject.put("type", cart.getType());
                    jsonObject.put("date_time", cart.getDate_time());
                    jsonArray.put(jsonObject);
                    obj_arr.add(jsonObject);
                }
                Log.e("JSONArray", String.valueOf(jsonArray));
            } catch (JSONException jse) {
                jse.printStackTrace();
            }




 String user_id = SharedPreferenceUtils.getString(getActivity(), 
 Const.USER_ID);
    RetrofitAPI retrofitAPI = 
 APIClient.getRetrofitInstance().create(RetrofitAPI.class);
    Call<StartResponseModal> call = 
 retrofitAPI.postData(user_id,obj_arr.toString());
    call.enqueue(new Callback<StartResponseModal>() {
        @Override
        public void onResponse(Call<StartResponseModal> call, 
  Response<StartResponseModal> response) {
            // Handle success
            if (response.isSuccessful() && 
  response.body().getErrorCode().equals("0")) {
                // Process the response here
                Log.e("Hello Room data send","Success");
                deleteItemsFromDatabase();


            } else {
                // Handle API error
                Log.e("Hello Room data send","failed else");

            }
        }

        @Override
        public void onFailure(Call<StartResponseModal> call, Throwable t) {
            // Handle failure
            Log.e("Hello Room send","failure");

        }
    });
Rakesh Jha
  • 279
  • 2
  • 7