android上传二进制文件大小,在Android中使用改进2上传二进制文件

我想在

Android中将二进制文件上传到服务器.我通过邮递员测试Api方法:

fP58X.png

没关系,你可以看到还有另一个选项可以上传文件作为表单数据(键,值):

V0Lbt.png

每个教程(如this one)都描述了如何将文件作为multipart / form-data上传:

// create RequestBody instance from file

RequestBody requestFile =

RequestBody.create(MediaType.parse("multipart/form-data"), file);

// MultipartBody.Part is used to send also the actual file name

MultipartBody.Part body =

MultipartBody.Part.createFormData("picture", file.getName(), requestFile);

我搜索了很多,但找不到任何方法上传文件作为二进制与retrofit2.

API服务:

@POST("trip/{tripId}/media/photos")

Call postEventPhoto(

@Path("eventId") int tripId,

@Header("Authorization") String accessToken,

@Query("direction") String direction,

@Body RequestBody photo);

呼叫者:

InputStream in = new FileInputStream(new File(media.getPath()));

byte[] buf;

buf = new byte[in.available()];

while (in.read(buf) != -1);

RequestBody requestBody = RequestBody

.create(MediaType.parse("application/octet-stream"), buf);

Call mediaPostCall = tripApiService.postTripPhoto(

tripId,

((GlobalInfo) getApplicationContext()).getApiAccessToken(),

direction,

requestBody);

但我得到了这个错误:

java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding.

我的代码出了什么问题?我该怎么办?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值