我使用改造连接到需要POST数据中的授权令牌的服务器。示例对于请求:
https://adress/inboxes
POST data:
token=jasdf807gb123uy40bviubva08sdyv123&message_id=1&
我使用这样的方法,它的工作正常:
@POST("/inboxes")
Call<InboxesResponse> getInbox(@Header("Authorization") String authorization, @Header("Content-Type") String contentType, @Body RequestBody body);
我将令牌和其他参数放入RequestBody:
RequestBody body =
RequestBody.create(MediaType.parse("text/plain"), text.toString());
这一切都很好。但我无法弄清楚如何使用文件上传方法:
Example:
https://adress/fileUpload
POST data:
token=jasdf807gb123uy40bviubva08sdyv123
File body
所以我需要使用MultipartBody.Part file
来上传文件,但是如何将MultipartBody与包含令牌的RequestBody结合起来呢?或者如何做到这一点?我很困惑..
答案 0 :(得分:0)
好的,答案是:
@Multipart
@POST("/fileUpload")
Call<ResponseBody> uploadFile(@Header("Authorization") String authorization,
@Part("token") RequestBody token,
@Part MultipartBody.Part file
);