Home » Android » android – Upload file with Retrofit 2.0

android – Upload file with Retrofit 2.0

Posted by: admin June 15, 2020 Leave a comment


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

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?


How to&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

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

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