How to POST a List as Body in Retrofit 2.6

3.9k Views Asked by At

Already searched a lot but couldn't find an appropriate answer. I have the following JSON. I understand this is a List object. How do I send a POST request as @Body in retrofit 2? Also, what is the POJO that I need to have to get a successful response from the API.

Please note that I have looked into all JSONObject based solutions. I am looking only for POJO based solutions where List/fields are sent as constructors.

{ 
   "ring":[ 
      { 
         "ring_pitch_id":"xxxx-xxxx-xxxx-xxxx",
         "ring_match_id":"xxxx-xxxx-xxxx-xxxx",
         "name":"xxxx",
         "type":"xxxx",
         "status":"xxxx"
      }
   ]
}
2

There are 2 best solutions below

0
On BEST ANSWER

The way I resolved this is by creating two model classes, ringList and ring. Similar to Ionut's answer, the ringList class contained List with setter. The ring model class contained all the 5 fields with getters and setters. In the calling method had the code that created object of ring class passing all 5 parameters and creating a list of it by writing List<ring> temp = new ArrayList<>();. temp.add(object_of_ring);. Creating object of ringList class and passing ring either as constructer or setter worked.

0
On

Here is your pojo.

This is body:

public class RingBody {
    List<RingModel> ring = new ArrayList<RingModel>();
}

This is items of list of your body.

public class RingModel {
    @SerializedName("ring_pitch_id")
    String ringPitchId;
    @SerializedName("ring_match_id")
    String ringMatchId;
    String name;
    String type;
    String status;
}