使用Retrofit上传文件并且携带参数,为什么这样不行呢?

1.Retrofit接口定义:

    @Multipart
    @POST(RequestUrl.BANK_CARD_INFO)
    Observable<BaseResponse<Object>> bankCardInfo(@Part MultipartBody.Part file);

2.使用:


                        String traceId = UUID.randomUUID().toString(); //随机生成唯一traceId
                        String path = selectList.get(0).getPath();
//                        Log.d(TAG, "onActivityResult: 1616=  " + path);
                        File file = new File(path);
                        RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), file);
//                        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        RequestBody traceBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//                        RequestBody fileRQ = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//                        MultipartBody.Part part = MultipartBody.Part.createFormData("picture", file.getName(), fileRQ);
                        Toast.makeText(this, "1933=   "+traceId, Toast.LENGTH_SHORT).show();
                        RequestBody bo=new MultipartBody.Builder()
                                .addFormDataPart("trace_id",traceId,traceBody)
                                .addFormDataPart("image",file.getName(),fileBody)
                                .build();

                        MultipartBody.Part myBody =
//                                MultipartBody.Part.createFormData();
                                MultipartBody.Part.createFormData("image", file.getName(), bo);   //name为传递文件的键

                        //todo:传递银行卡照片到智趣,返回银行卡信息
                        RxManager.getMethod().bankCardInfo(myBody)
                                .compose(RxUtil.schedulers(this))
                                .subscribe(new RxCallback<Object>(this) {
                                    @Override
                                    public void onSuccess(Object o, String msg) {
                                        Log.d(TAG, "onSuccess: 获取银行卡信息成功");
                                    }
                                });
                        break;
                }
阅读 5.8k
2 个回答

1.定义请求:1个header参数 + 1个请求参数 + 1个文件

public interface HttpService {
    @Multipart
    @POST("your_url")
    Observable<YourObject> func(
            @Header("your_http_header_1") String header,
            @Part("your_param_1") RequestBody param,
            @Part MultipartBody.Part file);
}

2.调用请求:请求参数为文本数据

final MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/plain");

RequestBody param = RequestBody.create(MEDIA_TYPE_TEXT, “your text”);
File file = ?? 
String contentType = ?? // 获取file对应的MimeType
RequestBody requestFile = RequestBody.create(MediaType.parse(contentType), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("your_file_1", file.getName(), requestFile);

Observable<YourObject> request = httpService.func("header1", param, body);

// TODO ...

多参数,多文件,同样适用于单文件单参数

    @Multipart
    @POST(RequestUrl.BANK_CARD_INFO)
    Observable<BaseResponse<Object>> bankCardInfo(@PartMap Map<String, RequestBody> map,
                                                  @Part MultipartBody.Part... files);
 // 参数初始化....
 Map<String, RequestBody> mapParams = new HashMap<>();
 RequestBody traceBody = RequestBody.create(MediaType.parse("multipart/form-data"), traceId);
 mapParams.put("trace_id", traceBody);
 
 RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), file);
 MultipartBody.Part myBody = MultipartBody.Part.createFormData("image", file.getName(), fileBody);
 // 请求
 RxManager.getMethod().bankCardInfo(mapParams, myBody)
         .compose(RxUtil.schedulers(this))
         .subscribe(new RxCallback<Object>(this) {
              @Override
              public void onSuccess(Object o, String msg) {
              Log.d(TAG, "onSuccess: 获取银行卡信息成功");
            }
        });
 ...
宣传栏