Java 我想通过改型multipart将图像和字符串数据都保存在服务器上,但出现了一个错误
我想通过改型multipart在服务器上保存图像和字符串数据,但我得到一个错误“500内部服务器错误” 这是 API在Postman上运行良好,但在改装时出现错误Java 我想通过改型multipart将图像和字符串数据都保存在服务器上,但出现了一个错误,java,android,retrofit,Java,Android,Retrofit,我想通过改型multipart在服务器上保存图像和字符串数据,但我得到一个错误“500内部服务器错误” 这是 API在Postman上运行良好,但在改装时出现错误 @Multipart @POST("item_app") Call<ItemAddResponse> addNewItem(@Part MultipartBody.Part file, @Part("name") RequestBody name
@Multipart
@POST("item_app")
Call<ItemAddResponse> addNewItem(@Part MultipartBody.Part file,
@Part("name") RequestBody name,
@Part("description") RequestBody description,
@Part("tags") RequestBody tags,
@Part("category") RequestBody category,
@Part("stock_status") RequestBody stock_status,
@Part("sku") RequestBody sku,
@Part("quantity") RequestBody quantity,
@Part("pcs_per_box") RequestBody pcs_per_box,
@Part("price") RequestBody price,
@Part("safe_quantity") RequestBody safe_quantity,
@Part("remarks") RequestBody remarks
);
此函数用于添加文件
@NonNull
public static MultipartBody.Part createFilePartFromFile(String partName, String file_path) {
MultipartBody.Part body = null;
if (file_path != null) {
File file = new File(file_path);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
body = MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
return body;
}
return null;
}
谢谢如果您的端点正确,则您用于创建文件部件的方法可能存在问题,请尝试以下操作:
pictureBody = MultipartBody.Part.createFormData("yourTag",
selectedFile.getName(), RequestBody.create(MediaType.parse("image/*"),
selectedFile));
请注意,我正在发送文件,而不是文件路径。您是否尝试使用postman访问API以确保其正常工作?
@NonNull
public static MultipartBody.Part createFilePartFromFile(String partName, String file_path) {
MultipartBody.Part body = null;
if (file_path != null) {
File file = new File(file_path);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
body = MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
return body;
}
return null;
}
pictureBody = MultipartBody.Part.createFormData("yourTag",
selectedFile.getName(), RequestBody.create(MediaType.parse("image/*"),
selectedFile));