You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Retrofit在Java中向服务器发送嵌套座位数据?

解决方案

你当前用@Query传递参数的方式不符合服务器要求——服务器需要的是JSON请求体,而非URL查询参数。要处理嵌套的seats数组,你需要通过以下步骤改造代码:

1. 创建对应JSON结构的数据类

先定义嵌套的Seat类,再定义包含所有请求字段的主请求类:

Kotlin 示例

// 对应seats数组中的单个对象
data class Seat(
    val seat_type: String,
    val seat_number: String,
    val type: String // 固定为"bus"即可,和请求示例一致
)

// 完整的请求体类
data class BookingRequest(
    val name: String,
    val age: Int,
    val gender: String,
    val citizen: String,
    val secondname: String,
    val secondphone: String,
    val transportation_id: Int,
    val date: String,
    val departure_time: String,
    val arrival_time: String,
    val from: Int,
    val to: Int,
    val type: String,
    val seats: List<Seat>
)

Java 示例(JavaBean)

public class Seat {
    private String seat_type;
    private String seat_number;
    private String type;

    // 构造方法、getter、setter
    public Seat(String seat_type, String seat_number, String type) {
        this.seat_type = seat_type;
        this.seat_number = seat_number;
        this.type = type;
    }

    // 省略getter和setter
}

public class BookingRequest {
    private String name;
    private int age;
    private String gender;
    private String citizen;
    private String secondname;
    private String secondphone;
    private int transportation_id;
    private String date;
    private String departure_time;
    private String arrival_time;
    private int from;
    private int to;
    private String type;
    private List<Seat> seats;

    // 构造方法、getter、setter
    // 省略具体实现
}

2. 修改Retrofit接口

将一堆@Query替换为@Body,直接传递整个请求对象,保留请求头参数:

@POST("bookinglogs")
fun bookSeat(
    @Body request: BookingRequest,
    @Header("Authorization") authToken: String,
    @Header("Accept") accept: String
): Observable<BookingResponse>

3. 组装请求数据并调用接口

利用你已有的座位类型、座位号列表,生成Seat集合,再创建完整的请求对象:

// 假设你已有的数据
val name = "Passenger 1"
val age = 22
val gender = "male"
val citizen = "35-01-02434"
// ...其他字段赋值
val seatTypes = listOf("leftB", "leftB")
val seatNumbers = listOf("5", "6")

// 组装seats列表
val seats = seatTypes.zip(seatNumbers).map { (type, number) ->
    Seat(type, number, "bus")
}

// 创建完整请求对象
val bookingRequest = BookingRequest(
    name = name,
    age = age,
    gender = gender,
    citizen = citizen,
    secondname = "",
    secondphone = "",
    transportation_id = 1,
    date = "2022-10-12",
    departure_time = "09:00:00",
    arrival_time = "",
    from = 1,
    to = 2,
    type = "bus",
    seats = seats
)

// 调用接口
apiService.bookSeat(bookingRequest, "你的AuthToken", "application/json")
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe({ response ->
        // 处理成功回调
    }, { error ->
        // 处理错误回调
    })

注意事项

  • 确保你的Retrofit实例已配置JSON转换器(如GsonConverterFactory.create()或MoshiConverterFactory.create()),否则无法自动将数据类序列化为JSON请求体。

内容的提问来源于stack exchange,提问作者Saurav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 15:30:58