10

I'm sending object in request body, something like that :

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

and it's work fine without the images. From the docs I can find how to upload file to server using MultipartBody.Part, my questions is :

  1. How can I upload multiple images at the same time?
  2. Is it possible to send the images inside the object, or I need to send it separately and how ?

thank you very much.

2 Answers 2

9

request success just now with server

I reference 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 multi files

may be this could help , I did not try


@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) { } }); } 

Sign up to request clarification or add additional context in comments.

2 Comments

thank you Michael Mao for your response, can you send multiple files at the same time or you know how to do?
hi @Abdel see the update answer :) I havanot try it .
6

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

RequestBody.create(MediaType.parse("application/json"), json) RequestBody.create(MediaType.parse("image/jpg"), file) 

2 Comments

can you please share retrofit client and spring controller method details?
as your case if i want to send Gson Model how can i do this ? @Piyush

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.