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

Retrofit POST请求报Bad Request错误:如何正确实现?

Retrofit POST请求返回400 Bad Request问题解决

问题原因

你当前代码使用@FormUrlEncoded以表单键值对方式提交数据,但你的API要求JSON格式请求体,结构为外层包裹data对象、内部包含title和body字段,服务器无法解析表单格式的请求,因此返回400错误。

解决方案步骤

1. 创建适配POST请求体的数据类

新增类封装API要求的{"data": {...}}JSON结构:

public class PostNoteRequest {
    @SerializedName("data")
    @Expose
    private Data data;

    public PostNoteRequest(Data data) {
        this.data = data;
    }

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }
}

2. 修改Retrofit接口

移除@FormUrlEncoded注解,改用@Body传递JSON请求体:

public interface Service {
    @POST("api/notes")
    Call<DataResponse> create(@Body PostNoteRequest request);
}

3. 调整POST请求调用逻辑

构造符合要求的请求体对象,再发起请求:

private void create(){
    crudInterface = RestClient.connection().create(Service.class);
    
    // 封装title和body到Data对象
    Data noteData = new Data();
    noteData.setTitle(title);
    noteData.setBody(body);
    
    // 构造完整请求体
    PostNoteRequest request = new PostNoteRequest(noteData);
    
    Call<DataResponse> call = crudInterface.create(request);
    call.enqueue(new Callback<DataResponse>() {
        @Override
        public void onResponse(@NonNull Call<DataResponse> call, @NonNull Response<DataResponse> response) {
            Toast toast = Toast.makeText(getApplicationContext(), response.message(), Toast.LENGTH_LONG);
            toast.show();
            Log.e("Response : ", response.message());
            Log.d("Response code: ", String.valueOf(response.code()));
        }

        @Override
        public void onFailure(@NonNull Call<DataResponse> call, @NonNull Throwable t) {
            Toast.makeText(getApplicationContext(), "Gagal menghubungi server : "+t.getMessage(), Toast.LENGTH_LONG).show();
            Log.e("Throw err: ",t.getMessage());
        }
    });
}

4. 修复现有数据类的逻辑bug

你的Notes类存在方法赋值错误,需修正:

public class Notes {
    @SerializedName("id")
    private int id;
    @SerializedName("attributes")
    private Data data;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Data getAttribute() {
        return data;
    }

    // 修正赋值逻辑:将传入的attribute赋值给成员变量
    public void setAttribute(Data attribute) {
        this.data = attribute;
    }

    // 实现重载方法的逻辑
    public void setAttribute(String title, String body) {
        if (this.data == null) {
            this.data = new Data();
        }
        this.data.setTitle(title);
        this.data.setBody(body);
    }
}

验证要点

确保你的Retrofit实例已配置JSON转换器(如GsonConverterFactory),否则无法自动将对象转为JSON:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("你的API基础地址")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 06:16:20