Retrofit + Rxjava 多图片上传

Retrofit + Rxjava 多图片上传

方法1:
@Multipart
@POST(“接口”)
Observable updateImages(@Part List parts);

List paths = new ArrayList();
MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)//表单类型
                .addFormDataPart("uuid", uuid)
                .addFormDataPart("token", token)
                .addFormDataPart("orderNum", orderNum)
                .addFormDataPart("bankCardNum", getEditTextString(etBankNum))
                .addFormDataPart("payType", payType)
                .addFormDataPart("fuyouNo", getEditTextString(etSerialNumber))
                .addFormDataPart("type", "pay")
                .addFormDataPart("id", id)
                .addFormDataPart("bankName", getEditTextString(etBankInformation));

        List parts = new ArrayList<>();

        for (String imgPath : paths) {
            File file = new File(imgPath);
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("files", file.getName(), requestFile);//files 后台接收图片流的参数名
            parts = builder.build().parts();
        }

方法2:
@Multipart
@POST(“接口”)
Observable updateImages(@PartMap Map params);

Map images = new HashMap<>();
        //一般参数
        images.put("uuid", RequestBody.create(MediaType.parse("text/plain"), uuid));
        images.put("token", RequestBody.create(MediaType.parse("text/plain"), token));
        images.put("orderNum", RequestBody.create(MediaType.parse("text/plain"), orderNum));
        images.put("bankCardNum", RequestBody.create(MediaType.parse("text/plain"), getEditTextString(etBankNum)));
        images.put("payType", RequestBody.create(MediaType.parse("text/plain"), payType));
        images.put("fuyouNo", RequestBody.create(MediaType.parse("text/plain"), getEditTextString(etSerialNumber)));
        images.put("type", RequestBody.create(MediaType.parse("text/plain"), "pay"));
        images.put("id", RequestBody.create(MediaType.parse("text/plain"), id));
        images.put("bankName", RequestBody.create(MediaType.parse("text/plain"), getEditTextString(etBankInformation)));
        //文件(注意与上面的不同)
        for (String imgPath : paths) {
            String neiPlayImg = ImageUtil.saveCompressImg(this, imgPath, "neiPlayImg");
            File file = new File(neiPlayImg);
            long fileSize = getFileSize(file);
            toFileSize(fileSize);
            images.put("files" + file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
        }
HttpUtil.getInstance()
                .getHttpService()
                .updateImages(parts)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver>() {
                    @Override
                    public void onNext(BaseHttpResult result) {
                        int code = result.getCode();
                        if (code == 0) {
                            ToastUtil.showToast(result.getMessage());
                            finish();
                        } else {
                            ToastUtil.showToast(result.getMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });

你可能感兴趣的:(Retrofit + Rxjava 多图片上传)