Skip to content Skip to sidebar Skip to footer

Retrofit - Send Request Body As Array Or Number

I'm using Retrofit 2 and I need to send request body. The problem is somehow the value is converted to string. On the example below, you can see that items and totalPrice which sho

Solution 1:

The conversion most certainly happens because you are using @FormUrlEncoded. According to the documentation:

Field names and values will be UTF-8 encoded before being URI-encoded in accordance to RFC-3986.

A solution would be to use a model class instead of a Map. I see you already have a Sale class. If it looks like something like this:

publicclassSale{
    String cashierId;
    int totalPrice;
    String paymentMethod;
    ArrayList<SomeObject> items;
}

you can simply do like this:

// in service@POST("api/sales")
Call<Sale> createSale(@Body Sale sale);

// when doing the callSalesale=newSale();
// set everything in your object// then
Call<Sale> call = retailService.createSale(sale);

Post a Comment for "Retrofit - Send Request Body As Array Or Number"