Search code examples
javaandroidhttpretrofit2retrofit2.6

How to POST a List as Body in Retrofit 2.6


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"
      }
   ]
}

Solution

  • 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.