Retrofit 2: sending files using json object

I am sending an object to the body of the request, something like this:

{ "title":"test", "description":"test", "images":[] } @POST("create-data") Call<JsonObject> publishData(@Body MyObject object); 

and it works fine without images. From the docs I can find how to upload a file to the server using MultipartBody.Part , my questions are:

  • How to upload multiple images at once?
  • Can I send images inside an object, or do I need to send it separately and how?

Thank you very much.

+7
json android web-services retrofit retrofit2
source share
2 answers

request success just now with the server

I refer to the article:

https://futurestud.io/blog/retrofit-2-how-to-upload-files-to-server


 @Multipart @POST("uploadHeadPic") Call<UploadHeadPicResponseModel> uploadHeadPic(@Part MultipartBody.Part file, @Part("json") RequestBody json); 

 public void doUploadHeadPic(@NonNull String filePath) { if (!MNetworkUtil.isNetworkAvailable()) { MToastUtil.show("网络不能连接"); return; } File file = new File(filePath); String json = new Gson().toJson(new UploadHeadPicRequestModel()); if (!file.exists()) { MToastUtil.show("文件不存在"); return; } progressDialog.show(); avatarSimpleDraweeView.setEnabled(false); MApiManager.getService().uploadHeadPic( MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file)), RequestBody.create(MediaType.parse("multipart/form-data"), json)) .enqueue(new OnRetrofitCallbackListener<UploadHeadPicResponseModel>(mActivity) { @Override public void onSuccess(UploadHeadPicResponseModel responseModel) { progressDialog.dismiss(); avatarSimpleDraweeView.setEnabled(true); if (responseModel != null) { String serverAvatarUrl = responseModel.data.headPicPath; if (!TextUtils.isEmpty(serverAvatarUrl)) { UserModel userModel = MUserManager.getInstance().getUser(); if (userModel != null) { userModel.setAvatarUrl(serverAvatarUrl); MUserManager.getInstance().updateOrInsertUserInfo(userModel); MToastUtil.show("上传头像成功"); } } } } @Override public void onFailure(int status, String failureMsg) { progressDialog.dismiss(); avatarSimpleDraweeView.setEnabled(true); MToastUtil.show((TextUtils.isEmpty(failureMsg) ? "上传失败" : failureMsg) + " : " + status); } }); } 

update for multiple files

Perhaps this may help, I have not tried


 @Multipart @POST("uploadHeadPic") Call<UploadHeadPicResponseModel> uploadHeadPic(@Part MultipartBody.Part file0, @Part MultipartBody.Part file1, @Part("json") RequestBody json); 


 public void doUploadHeadPic(@NonNull String filePath) { MApiManager.getService().uploadHeadPic( MultipartBody.Part.createFormData("file0", file0.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file0)), MultipartBody.Part.createFormData("file1", file1.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file1)), RequestBody.create(MediaType.parse("multipart/form-data"), json)) .enqueue(new OnRetrofitCallbackListener<UploadHeadPicResponseModel>(mActivity) { @Override public void onSuccess(UploadHeadPicResponseModel responseModel) { } @Override public void onFailure(int status, String failureMsg) { } }); } 

+5
source share

In my case (uploading to a server built using Spring) I needed to change MediaType for RequestBody :

 RequestBody.create(MediaType.parse("application/json"), json) RequestBody.create(MediaType.parse("image/jpg"), file) 
+3
source share

All Articles