Android Retrofit | Post custom object (send json to server)
The error is from not providing the @Body
annotation on your Order
parameter. Change it to:
@POST(URL_ORDERS)public void newOrder(@Body Order order, Callback<Boolean> success);
Retrofit uses Gson to serialize and deserialize JSON by default. Gson uses variable names by default for serialization, but they can be changed using the annotation @SerializedName("replacement_name")
.
For Example, If your Order
class looked like this:
public class Order { @SerializedName("custom_id") private int id; private String name; private List<Item> items;}public class Item { private int id; private String name;}
Then Gson would automatically serialize that to
{ "custom_id": 1, "name": "Hello Object", "items": [ { "id": 1, "name": "Hello Item" } ]}