1.Retrofit接口定义:@Multipart@POST(RequestUrl.BANK_CARD_INFO)ObservablebankCardInfo(@PartMultipartBody.Partfile);2.使用:StringtraceId=UUID.randomUUID().toString();//随机生成唯一traceIdStringpath=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){@OverridepublicvoidonSuccess(Objecto,Stringmsg){Log.d(TAG,"onSuccess:获取银行卡信息成功");}});break;}
2 回答
守候你守候我
TA贡献1802条经验 获得超10个赞
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...
添加回答
举报
0/150
提交
取消