Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2 : send files with json object

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.

like image 588
Abdel Avatar asked May 16 '16 12:05

Abdel


2 Answers

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

like image 188
Michael Mao Avatar answered Nov 16 '22 17:11

Michael Mao


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)
like image 6
Olek Avatar answered Nov 16 '22 16:11

Olek