Retrofit 2.0 rxJava2.0上传图片

和参数一起  单个图片(图片为空)
MultipartBody.Part body;
String descriptionString;
RequestBody description;
if (mPicListfile != null && mPicListfile.size() == 1) {
    //构建要上传的文件
    File file = mPicListfile.get(0);
    RequestBody requestFile =
            RequestBody.create(MediaType.parse("application/otcet-stream"), file);

    body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);

} else {
    RequestBody requestFile =
            RequestBody.create(MediaType.parse("application/otcet-stream"), "");
    body = MultipartBody.Part.createFormData("file", "", requestFile);
}

descriptionString = "file";

description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);

HttpManager.getInstance().create(PluginApi.class)
        .saveOwner(description,body/*parts*/, "ANDROID", id, ownerProjectNo, ownerId, code)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer>() {
            @Override
            public void accept(RequestEntity dataRequestEntity) throws Exception {
                LoadingDialogManager.getInstance().dismiss();
                if (dataRequestEntity.errcode == 0) {
                    ToastUtil.getInstance(mContext).show("提交成功");
                    content_linear.setVisibility(View.GONE);
                }
            }

        }, new Consumer() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                Logger.e(throwable.getMessage());
                ToastUtil.getInstance(mContext).show("请求失败");
                LoadingDialogManager.getInstance().dismiss();
            }
        });
@Multipart
@POST(ConfigManager.SERVER_BASE_URL2 + "WeChatPartAConfirm/saveOwner.do")
Flowable> saveOwner(@Part("description") RequestBody description, @Part MultipartBody.Part file, @Query("sourceType") String sourceType, @Query("id") int assignmentId, @Query("ownerProjectNo") String ownerProjectNo, @Query("ownerId") int ownerId, @Query("code") String code);
多个图片
    LoadingDialogManager.getInstance().show(mContext);

//        Map map = new HashMap<>();
//        map.put("sourceType", "ANDROID");
//        map.put("applicationId", applicationId);
//        map.put("resultMsg_assignment", resultMsg_assignment);
//        map.put("userId", personID);
//        map.put("personName", personName);
//        map.put("advive", advive);
//        map.put("id", id);
//        map.put("name", title);
//        map.put("personId", mpersonId);
        if (mPicList != null && mPicList.size() > 0) {
            for (int i = 0; i < mPicList.size(); i++) {
                if (!mPicList.get(i).startsWith("http")) {
                    mPicListfile.add(new File(mPicList.get(i)));
                }
            }
        }
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
        for (int i = 0; i < mPicListfile.size(); i++) {
            File file = mPicListfile.get(i);
            LogUtils.LOGI("tag===", file.getName() + "==" + file.getAbsolutePath());
            RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/jpg"), file);
            builder.addFormDataPart("file" + i, file.getName(), photoRequestBody);
        }
        List parts = builder.build().parts();

        HttpManager.getInstance().create(PluginApi.class)
                .uploadImgs(parts, "ANDROID", id, title, processLocation, startData, endData, useTime, resultDescribe, projectId, personID, personName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer>() {
                    @Override
                    public void accept(RequestEntity dataRequestEntity) throws Exception {
                        LoadingDialogManager.getInstance().dismiss();
                        if (dataRequestEntity.errcode == 0) {
                            ToastUtil.getInstance(mContext).show("提交成功");
                            backLogRefListener.refListener();
                            finish();
                        }
                    }

                }, new Consumer() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Logger.e(throwable.getMessage());
                        ToastUtil.getInstance(mContext).show("请求失败");
                        LoadingDialogManager.getInstance().dismiss();
                    }
                });
@Multipart
@POST(ConfigManager.SERVER_BASE_URL2 + "assignment/saveAssignmentResult.do")
Flowable> uploadImgs(@Part List file, @Query("sourceType") String sourceType, @Query("assignmentId") int assignmentId, @Query("assignmentName") String assignmentName, @Query("processLocation") int processLocation, @Query("beginTime") String beginTime, @Query("endTime") String endTime, @Query("useTime") String useTime, @Query("resultDescribe") String resultDescribe, @Query("projectId") int projectId, @Query("userId") String userId, @Query("personName") String personName);

你可能感兴趣的:(Retrofit 2.0 rxJava2.0上传图片)