上传图片及其他类型的参数

1.依赖

2.

@POST("Apply/subapply")
Observable<BaseResponse<CommitInfoBean>> commitinfos(@Body RequestBody body);

3.p层

public void commitinfo(String type, String name, String phone, int mid,
                       String province, String city, String district, String village,
                       String address, File idcard1, File idcard2, File prove) {

    String decode = (String) SPUtil.get(mContext, Contants.APP_DECODE, "");
    if (TextUtils.isEmpty(decode)) {
        decode = "";
    }

    //构建body
    RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
            .addFormDataPart("type", type)
            .addFormDataPart("decode", (String) SPUtil.get(mContext,Contants.APP_DECODE,""))
            .addFormDataPart("name", name)
            .addFormDataPart("phone", phone)
            .addFormDataPart("mid", String.valueOf(mid))
            .addFormDataPart("province", province)
            .addFormDataPart("city", city)
            .addFormDataPart("district", district)
            .addFormDataPart("village", village)
            .addFormDataPart("address", address)
            .addFormDataPart("idcard1", idcard1.getName(), 
RequestBody.create(MediaType.parse("image/*"), idcard1))
            .addFormDataPart("idcard2", idcard2.getName(), 
RequestBody.create(MediaType.parse("image/*"), idcard2))
            .addFormDataPart("prove", prove.getName(), 
RequestBody.create(MediaType.parse("image/*"), prove))
            .build();


    Api.getServer().commitinfos(requestBody)
            .compose(schedulersTransformer())
            .subscribe(new HttpObserver<CommitInfoBean>(mContext) {
                @Override
                protected void onSuccess(CommitInfoBean o) {
                    mView.callbackcommitinfo(o);
                }

                @Override
                protected void onFail(Throwable e) {
                    mView.onFail("" + e.getMessage());
                }
            });


}

4.获得图片路径(string类型的),放到事先写好的方法中

private void compressedPicture(final String result) {
    Luban
            .with(mContext)
            .load(result)
            .setCompressListener(new OnCompressListener() {
                @Override
                public void onStart() {
                    Log.e(TAG, "onStart: 开始压缩" + result);
                }

                @Override
                public void onSuccess(File file) {

                    switch (picture) {
                        case 1: {
                            DefaultAlbumLoader
                                    .getInstance()
                                    .loadImage(ivIdcardpicture1Myinfo, file.getPath(), 720, 1280);
                            file1 = file;
                        }
                        break;
                        case 2: {
                            DefaultAlbumLoader
                                    .getInstance()
                                    .loadImage(ivIdcardpicture2Myinfo, file.getPath(), 720, 1280);
                            file2 = file;
                        }
                        break;
                        case 3: {
                            DefaultAlbumLoader
                                    .getInstance()
                                    .loadImage(ivHousepictureMyinfo, file.getPath(), 720, 1280);
                            file3 = file;
                        }
                        break;
                        default:

                    }
                }

                @Override
                public void onError(Throwable e) {
                    Log.e(TAG, "onError: 压缩失败" + result + "==" + e.getMessage());

                }
            }).launch();
}

5.请求接口:

mPresenter.commitinfo(select, name, phone, Integer.valueOf(mid),
        shengNameIntent, shiNameIntent, xianNameIntent, village, address, file1,
        file2, file3);

6.v层只是一个回调

猜你喜欢

转载自blog.csdn.net/hisunyl/article/details/80581914