我有POST API,我在我的android源代码中使用了retroFit,这是POSTMan中的API参数这是header:enter image description here
这是Body:
这是我的伪码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个格式错误的请求。
发布于 2020-05-26 15:30:04
我的建议是使用数据模型类,如下所示。
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"})https://stackoverflow.com/questions/62001729
复制相似问题