Skip to content Skip to sidebar Skip to footer

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 publishData(@Bod

Solution 1:

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

publicvoiddoUploadHeadPic(@NonNullString filePath) {
    if (!MNetworkUtil.isNetworkAvailable()) {
        MToastUtil.show("网络不能连接");
        return;
    }
    File file = newFile(filePath);
    String json = newGson().toJson(newUploadHeadPicRequestModel());
    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(newOnRetrofitCallbackListener<UploadHeadPicResponseModel>(mActivity) {
                @OverridepublicvoidonSuccess(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("上传头像成功");
                            }
                        }
                    }
                }

                @OverridepublicvoidonFailure(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);


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

Solution 2:

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)

Post a Comment for "Retrofit 2 : Send Files With Json Object"