I want to upload a binary file to a server in Android. I check the Api method by the postman:

And this is normal, as you can see, there is another option that you can upload files in the form of form data (key, value):

Each tutorial (like this one ) describes how to upload files like 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);
I am looking a lot, but cannot find a way to download the file in binary using retrofit2.
There is only one problem in the updated repository, which asks how I can place the image binary in a modified beta 2.0? I use his solution:
API Service:
@POST("trip/{tripId}/media/photos")
Call<MediaPost> postEventPhoto(
@Path("eventId") int tripId,
@Header("Authorization") String accessToken,
@Query("direction") String direction,
@Body RequestBody photo);
Subscriber:
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<MediaPost> mediaPostCall = tripApiService.postTripPhoto(
tripId,
((GlobalInfo) getApplicationContext()).getApiAccessToken(),
direction,
requestBody);
But I got this error:
java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding.
What is wrong with my code? what should I do?