我有POST API,我在我的android源代码中使用retroFit,这是POSTMan中的API参数 这是标题: enter image description here
这是身体:
这是我的伪代码Android API接口:
@FormUrlEncoded
@Headers({"Content-Type: multipart/form-data", "Authorization: F31daaw313415"})
@POST("api/store/order")
Call<List<PostCommandResponse>> setCommandeProduct(@FieldMap Map<String, Object> test);
这是我的地图
data.put("product_id",productId);
data.put("adress", RequestBody.create(MediaType.parse("multipart/form-data"),adresse));
data.put("city",RequestBody.create(MediaType.parse("multipart/form-data"),city));
data.put("zip",RequestBody.create(MediaType.parse("multipart/form-data"),zip));
data.put("first_name",RequestBody.create(MediaType.parse("multipart/form-data"),surname));
data.put("last_name",RequestBody.create(MediaType.parse("multipart/form-data"),lastname));
但总是响应是400个格式错误的请求。
答案 0 :(得分:0)
我的建议是使用数据模型类,如下所示。
public class UserInfo {
@SerializedName("product_id")
@Expose
private Integer productId;
@SerializedName("address")
@Expose
private String address;
@SerializedName("city")
@Expose
private String city;
@SerializedName("zip")
@Expose
private String zip;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("last_name")
@Expose
private String lastName;
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
在此模型UserInfo
中设置值,并将其传递给API调用,如下所示。
@Headers({"Content-Type: multipart/form-data", "Authorization: Bearer F31daaw313415"})
@POST("api/store/order")
Call<List<PostCommandResponse>> setCommandeProduct(@Body UserInfo test);
只是一个小小的更正:还检查是否需要使用JSON发送地址或地址。 地址已在JSON主体图片中使用。相应地更改。
编辑:您发送的标头格式也不正确。
应该是这样的。
@Headers({"Content-Type: multipart/form-data", "Authorization: Bearer F31daaw313415"})