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

1.Retrofit接口定义:
@Multipart
@POST(RequestUrl.BANK_CARD_INFO)
Observable>bankCardInfo(@PartMultipartBody.Partfile);
2.使用:
StringtraceId=UUID.randomUUID().toString();//随机生成唯一traceId
Stringpath=selectList.get(0).getPath();
//Log.d(TAG,"onActivityResult:1616="+path);
Filefile=newFile(path);
RequestBodyfileBody=RequestBody.create(MediaType.parse("image/*"),file);
//RequestBodyrequestBody=RequestBody.create(MediaType.parse("multipart/form-data"),file);
RequestBodytraceBody=RequestBody.create(MediaType.parse("multipart/form-data"),file);
//RequestBodyfileRQ=RequestBody.create(MediaType.parse("multipart/form-data"),file);
//MultipartBody.Partpart=MultipartBody.Part.createFormData("picture",file.getName(),fileRQ);
Toast.makeText(this,"1933="+traceId,Toast.LENGTH_SHORT).show();
RequestBodybo=newMultipartBody.Builder()
.addFormDataPart("trace_id",traceId,traceBody)
.addFormDataPart("image",file.getName(),fileBody)
.build();
MultipartBody.PartmyBody=
//MultipartBody.Part.createFormData();
MultipartBody.Part.createFormData("image",file.getName(),bo);//name为传递文件的键
//todo:传递银行卡照片到智趣,返回银行卡信息
RxManager.getMethod().bankCardInfo(myBody)
.compose(RxUtil.schedulers(this))
.subscribe(newRxCallback(this){
@Override
publicvoidonSuccess(Objecto,Stringmsg){
Log.d(TAG,"onSuccess:获取银行卡信息成功");
}
});
break;
}

			
繁花不似锦
浏览 354回答 2
2回答

守候你守候我

1.定义请求:1个header参数+1个请求参数+1个文件publicinterfaceHttpService{@Multipart@POST("your_url")Observablefunc(@Header("your_http_header_1")Stringheader,@Part("your_param_1")RequestBodyparam,@PartMultipartBody.Partfile);}2.调用请求:请求参数为文本数据finalMediaTypeMEDIA_TYPE_TEXT=MediaType.parse("text/plain");RequestBodyparam=RequestBody.create(MEDIA_TYPE_TEXT,“yourtext”);Filefile=??StringcontentType=??//获取file对应的MimeTypeRequestBodyrequestFile=RequestBody.create(MediaType.parse(contentType),file);MultipartBody.Partbody=MultipartBody.Part.createFormData("your_file_1",file.getName(),requestFile);Observablerequest=httpService.func("header1",param,body);//TODO...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript