Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file with Retrofit 2.0

I'm using this method to upload files to my server:

@Multipart
@POST("new")
Call<Response> send(@Part("myFile") byte[] file);

I have been reading and some people use TypedFile to do it, and maybe this is easier than send raw bytes like I'm doing.

TypedFile class is in retrofit.mime package. But I don't have it there. Has this package been removed from 2.0 version? Or do I have to add another dependency? If so, which one?

Thanks.

like image 387
Héctor Avatar asked Oct 29 '15 15:10

Héctor


People also ask

How do I send a binary file in retrofit?

I use its 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);


1 Answers

In 2.0 you need to use RequestBody instead of TypedFile. Get file with RequestBody

RequestBody file = RequestBody.create(MediaType.parse("image/*"), path);    

Use it in your request

@Multipart
@POST("new")
Call<Response> send(@Part("myFile") RequestBody file);

More info https://github.com/square/retrofit/issues/1063

like image 65
HellCat2405 Avatar answered Oct 07 '22 00:10

HellCat2405