Reputation: 63
I can't send json array to server. When I test in postman raw, it is ok, success return.
Postman Raw;
[
{
"product_id": 2,
"name": "Umbrella",
"price": 200,
"quantity": 1,
"totalprice": 200,
"user_id": 1
},
{
"product_id": 1,
"name": "Apple",
"price": 200,
"quantity": 1,
"totalprice": 200,
"user_id": 1
}
]
APIInterface;
@POST("example/api/order")
Call<JSONArray> postOrder(@Body JSONArray jsonArray);
CartActivity;
try {
JSONArray jsonArray = new JSONArray();
for (Cart cart : cartList) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("product_id", cart.getProduct_id());
jsonObject.put("name", cart.getName());
jsonObject.put("price", cart.getPrice());
jsonObject.put("quantity", cart.getQuantity());
jsonObject.put("totalprice", cart.getTotalprice());
jsonObject.put("user_id", cart.getUser_id());
jsonArray.put(jsonObject);
}
Log.e("JSONArray", String.valueOf(jsonArray));
} catch (JSONException jse) {
jse.printStackTrace();
}
Log;
E/JSONArray: [{"product_id":1,"name":"Umbrella","price":200,"quantity":1,"totalprice":200,"user_id":1},{"product_id":2,"name":"Apple","price":89,"quantity":1,"totalprice":89,"user_id":1}]
Error Message from server;
{"values":[{"nameValuePairs":{"product_id":1,"name":"Umbrella","price":200,"quantity":1,"totalprice":200,"user_id":1}},{"nameValuePairs":{"product_id":2,"name":"Apple","price":89,"quantity":1,"totalprice":89,"user_id":1}}]}
Upvotes: 6
Views: 12417
Reputation: 379
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;
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");
}
});
Upvotes: 0
Reputation: 245
You can directly send the array of objects as parameter. Retrofit will handle the conversion. Change your interface method like this:
@POST("example/api/order")
Call<JSONArray> postOrder(@Body List<Cart> cartList);
Check this link, you will get an idea.
Upvotes: 10
Reputation: 1976
send like this:-
@POST("example/api/order")
Call<JSONArray> postOrder(@Query ("data") JSONArray jsonArray);
Upvotes: 0