改造2:使用json对象发送文件 [英] Retrofit 2 : send files with json object

查看:76
本文介绍了改造2:使用json对象发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在请求主体中发送对象,诸如此类:

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

{
  "title":"test",
  "description":"test",
  "images":[]
}

@POST("create-data")
Call<JsonObject> publishData(@Body MyObject object);

,并且没有图像也可以正常工作.从文档中,我可以找到如何使用MultipartBody.Part将文件上传到服务器,我的问题是:

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. 如何同时上传多张图片?
  2. 是否可以将图像发送到对象内部,还是需要单独发送?如何发送?

非常感谢.

推荐答案

服务器刚刚请求成功

我引用了这篇文章:

request success just now with server

I reference the article:

https://futurestud.io/blog/retrofit-2-如何将文件上传到服务器

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

这篇关于改造2:使用json对象发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆