如何 POST 在 Retrofit 2.6 中将列表作为正文

How to POST a List as Body in Retrofit 2.6

已经搜索了很多但找不到合适的答案。我有以下 JSON。我知道这是一个 List 对象。如何在改造 2 中将 POST 请求作为 @Body 发送?另外,我需要什么 POJO 才能从 API.

获得成功响应

请注意,我已经研究了所有 JSON 基于对象的解决方案。我只寻找基于 POJO 的解决方案,其中 List/fields 作为构造函数发送。

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

这是你的 pojo。

这是body:

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

这是您的body列表项。

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

我解决这个问题的方法是创建两个模型 classes,ringList 和 ring。与 Ionut 的回答类似,ringList class 包含带有 setter 的列表。环模型 class 包含所有 5 个带有吸气剂和 setter 的字段。在调用方法中,创建环 class 对象的代码传递了所有 5 个参数,并通过编写 List<ring> temp = new ArrayList<>();. temp.add(object_of_ring); 创建了它的列表。创建 ringList class 的对象并将环作为构造函数或 setter 传递。