I am trying to post below JSON array on server.
{
\"order\": [{
\"orderid\": \"39\",
\"dishid\": \"54\",
\"quantity\"
Create OrderRequest Class
public class OrderRequest {
@SerializedName("order")
public List orders;
}
create Order Class
public class Order {
@SerializedName("orderid")
public String Id;
}
EndPoint
public interface ApiEndpoint{
@POST("order")
Call createOrder(@Body OrderRequest order, @HeaderMap HashMap headerMap);
}
Use this type of implementation in the mainActivity which call to service
HashMap hashMap= new HashMap();
hashMap.put("Content-Type","application/json;charset=UTF-8");
OrderRequest orderRequest = new OrderRequest();
List orderList = new ArrayList();
Orders order = new Order();
order.Id = "20";
orderList.add(order);
//you can add many objects
orderRequest.orders = orderList;
Call dataResponse= apiEndPoint.createOrder(orderRequest,hashMap)
dataResponse.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
try{
}catch (Exception e){
}
}
In the createOrder method we donot need to convert object into Json. Because when we build retrofit we add converter factory as a GsonConverterFactory. It will automatic convert that object to the JSON
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();