如何通过改造发布ArrayList <Object>?

时间:2019-12-04 16:09:59

标签: java post retrofit2

我想向服务器发送这样的内容

{
      "tourId": 5,
      "stopPoints": [
          {
              "arrivalAt": 15532956000,
              "lat": 10.826125820975317,
              "leaveAt": 1553299200000,
              "long": 106.68923623859882,
              "maxCost": 100000,
              "minCost": 50000,
              "name": "Bun new",
              "serviceTypeId": 1
          },
          {
              "arrivalAt": 15532956011,
              "lat": 13.826125820975317,
              "leaveAt": 1553299200000,
              "long": 100.68923623859882,
              "maxCost": 150000,
              "minCost": 10000,
              "name": "Bun update",
              "serviceTypeId": 1
          },
       ],

}

这是我的服务界面

@FormUrlEncoded
    @POST("tour/set-stop-points")
    Call<serviceStopPoints> stopPointsSet(@Body serviceStopPoints serviceStopPoints);

这是stopPoint类

public class stopPoint {
    @SerializedName("arrivalAt")
    @Expose
    private long arrivalAt;
    @SerializedName("lat")
    @Expose
    private double lat;
    @SerializedName("leaveAt")
    @Expose
    private long leaveAt;
    @SerializedName("long")
    @Expose
    private double lng;
    @SerializedName("maxCost")
    @Expose
    private long maxCost;
    @SerializedName("minCost")
    @Expose
    private long minCost;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("serviceTypeId")
    @Expose
    private int serviceTypeId;
}

这是serviceStopPoints类

public class serviceStopPoints extends Number {
    @SerializedName("tourId")
    @Expose
        String tourID;
    @SerializedName("stopPoints")
    @Expose
    ArrayList<stopPoint> stopPoints;
}

我已阅读此链接并尝试 Sending ArrayList<Object> POST request with Retrofit 但我不明白这行

 Call<ServiceResult<Integer>> orderSet(@Body Order order);

请帮助我发送带有翻新的ArrayList。 感谢您阅读我的问题。

2 个答案:

答案 0 :(得分:0)

将get / set添加到您的班级,并 创建您的请求对象。

serviceStopPoints request = new serviceStopPoints();
List<stopPoint> stopPointList = new ArrayList<stopPoint>();
stopPoint s = new stopPoint();
s.setArrivalAt("..");
.
.
.
stopPointList.add(s);
request.setTourID("1234);
request.setStopPoints(stopPointList);

用法

 ApiHelper.getService().stopPointsSet(request).enqueue(new Callback<serviceStopPoints>() {
            @Override
            public void onResponse(Call<serviceStopPoints> call, Response<serviceStopPoints> response) {
                //do with the response
            }

            @Override
            public void onFailure(Call<serviceStopPoints> call, Throwable t) {
               //error
            }
        });

我认为您不需要@FromUrlEncoded。

希望这个帮助:)

答案 1 :(得分:0)

这是我的代码,但是它不起作用,你知道这是怎么回事

Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl("url")
                .addConverterFactory( GsonConverterFactory.create());
    Retrofit retrofit = builder.build();
    Api api = retrofit.create(Api.class);
        Call<serviceStopPoints> call = api.stopPointsSet(serviceStopPoint);
        call.enqueue(new Callback<serviceStopPoints>() {
            @Override
            public void onResponse(Call<serviceStopPoints> call, Response<serviceStopPoints> response) {
//do sth

}
            @Override
            public void onFailure(Call<serviceStopPoints> call, Throwable t) {

            }


        });