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

如何用Retrofit 2发送Post对象?请求格式错误求助

解决Retrofit请求的@Body与表单编码冲突及Bad Request问题

嘿,作为Android新手遇到这种问题太正常了,我来帮你一步步理清问题根源和解决办法:

第一个错误:@Body parameters cannot be used with form or multi-part encoding

这个是核心冲突点:你在接口里同时用了@FormUrlEncoded和@Body注解,但这俩在Retrofit里是互斥的——

  • @FormUrlEncoded是用来发送application/x-www-form-urlencoded格式的表单键值对(类似网页表单提交的格式)
  • @Body是用来发送application/json格式的嵌套请求体(正好匹配你需要的userinfo:{...}, Password:...结构)

所以第一步必须删掉@FormUrlEncoded注解,修正你的ApiInterface:

@POST("UserRegistration")
Call<UserRegistration> getUserRegistration(@Body UserRegistration userRegistration);

第二个错误:Bad Request(请求格式不匹配)

解决完上面的冲突后,Bad Request大概率是因为你的Model类字段和API要求的键名完全不匹配,导致生成的JSON格式不符合接口预期。

你需要用Gson的@SerializedName注解来强制映射字段名,确保和API要求的键名完全一致:

1. 修正UserRegistration类

API要求的外层键是userinfo、Password、PasswordConfirm、TokenStr,但你的类里用了items作为用户信息的字段名,必须映射过来:

import com.google.gson.annotations.SerializedName;

public class UserRegistration {
    @SerializedName("userinfo") // 映射到API需要的userinfo键
    User userInfo;
    @SerializedName("Password")
    String password;
    @SerializedName("PasswordConfirm")
    String passwordConfirm;
    @SerializedName("TokenStr")
    String tokenStr;

    public UserRegistration(User userInfo, String password, String passwordConfirm, String tokenStr) {
        this.userInfo = userInfo;
        this.password = password;
        this.passwordConfirm = passwordConfirm;
        this.tokenStr = tokenStr;
    }
}

2. 修正User类

你要发送的userinfo结构是{ Name="sample", Company="sample", Contact="Contact" },但你的User类里的字段名(比如COMPANY、CONTACT_NO)和API要求的完全不匹配,必须调整:

import com.google.gson.annotations.SerializedName;

public class User {
    @SerializedName("Name")
    private String name;
    @SerializedName("Company")
    private String company;
    @SerializedName("Contact")
    private String contact;

    // 只保留API需要的字段,避免多余数据干扰请求
    public User(String name, String company, String contact) {
        this.name = name;
        this.company = company;
        this.contact = contact;
    }
}

3. 确保Retrofit配置了JSON转换器

最后要确认你的ApiClient里添加了Gson转换器,不然Retrofit没法把你的Model对象转换成JSON格式:

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {
    private static Retrofit retrofit;
    private static final String BASE_URL = "你的API基础地址"; // 比如http://xxx.com/api/

    public static Retrofit getClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create()) // 必须添加这个转换器
                    .build();
        }
        return retrofit;
    }
}

最后调整请求代码

根据修正后的Model类,调整对象构造逻辑:

// 构造符合API要求的User对象
User userReg = new User("sampleName", "sampleCompany", "sampleContact");
// 构造UserRegistration对象
UserRegistration sendReg = new UserRegistration(userReg, "", "", token);
// 发起请求
final Call<UserRegistration> tableCall = apiService.getUserRegistration(sendReg);
// 后续的enqueue回调逻辑保持不变

这样调整后,Retrofit会生成完全符合API要求的JSON请求体,应该就能解决Bad Request问题了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:50:19