检查代码中是否正确设置请求头和请求体参数。使用以下示例代码作为参考:
String json = "{\"name\": \"John Smith\", \"age\": 30}";
Request request = new Request.Builder()
.url("https://example.com/api")
.addHeader("Content-Type", "application/json")
.post(RequestBody.create(MediaType.parse("application/json"), json))
.build();
Response response = client.newCall(request).execute();
在以上示例中,我们通过设置请求头Content-Type
为application/json
,并使用RequestBody
将JSON字符串作为请求体参数传递给API接口。确保请求头和请求体参数的设置与API接口要求一致,即可解决API动作POST复杂数据接收到null的问题。