我必须上载带有图像的多个产品信息,每个产品具有name
,quantity
,price
,image
(filePath)。产品数组将通过items
键发送。(请参见所附图片)
ProductModel
public class ProductModel {
private String itemName;
private String itemDesc;
private int qty = 1;
private String itemImage;
public String getItemImage() {
return itemImage;
}
public void setItemImage(String itemImage) {
this.itemImage = itemImage;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public String getItemDesc() {
return itemDesc;
}
public void setItemDesc(String itemDesc) {
this.itemDesc = itemDesc;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
}
请帮助我向服务器发送请求。
答案 0 :(得分:0)
您必须使用MultipartBody.Part
进行改造才能上传多张图像。尝试以下代码:
public interface WebServicesAPI {
@Multipart
@POST("url")
Call<YourModel> upload(@Part MultipartBody.Part name, @Part MultipartBody.Part desc, @Part MultipartBody.Part image);
}
这是上传文件的方法。
private void upload() {
MultipartBody.Part name= MultipartBody.Part.createFormData("name", productModel.getName());
MultipartBody.Part desc= MultipartBody.Part.createFormData("desc", productModel.getDesc());
File propertyImageFile = new File(productModel.getPropertyImagePath());
RequestBody propertyImage = RequestBody.create(MediaType.parse("image/*"), propertyImageFile);
MultipartBody.Part propertyImagePart = MultipartBody.Part.createFormData("image", propertyImageFile.getName(), propertyImage);
final WebServicesAPI webServicesAPI = RetrofitManager.getInstance().getRetrofit().create(WebServicesAPI.class);
Call<UploadSurveyResponseModel> response = null;
response = webServicesAPI.upload(name, desc, propertyImagePart);
response.enqueue(this);
}