I am trying to do a HTTP POST to server using Retrofit 2.0
MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/plain");
MediaType MEDIA_TYPE_IMAGE = MediaType.parse("image/*");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
profilePictureByte = byteArrayOutputStream.toByteArray();
Call<APIResults> call = ServiceAPI.updateProfile(
RequestBody.create(MEDIA_TYPE_TEXT, emailString),
RequestBody.create(MEDIA_TYPE_IMAGE, profilePictureByte));
call.enqueue();
The server returns an error saying the file is not valid.
This is weird because I have tried to upload the same file with the same format on iOS(using other library), but it uploads successfully.
I am wondering what is the proper way to upload an image using Retrofit 2.0?
Should I save it to disk first before uploading?
Thank you!
P.S.: I have used retrofit for other Multipart request that does not include image and they completed successfully. The problem is when I am trying to include a byte to the body.
Update Code for image file uploading in Retrofit2.0
Change
MediaType.parse("image/*")
toMediaType.parse("image/jpeg")
Adding to the answer given by @insomniac. You can create a
Map
to put the parameter forRequestBody
including image.Code for Interface
Code for Java class
So its very simple way to achieve your task. You need to follow below step :-
1. First step
You need to make the entire call as
@Multipart request
.item
andimage number
is just string body which is wrapped inRequestBody
. We use theMultipartBody.Part class
that allows us to send the actual file name besides the binary file data with the request2. Second step
Now you have
image path
and you need to convert intofile
.Now convertfile
intoRequestBody
using methodRequestBody.create(MediaType.parse("multipart/form-data"), file)
. Now you need to convert yourRequestBody requestFile
intoMultipartBody.Part
using methodMultipartBody.Part.createFormData("Image", file.getName(), requestBody);
.ImageNumber
andItemId
is my another data which I need to send to server so I am also make both thing intoRequestBody
.For more info
There is a correct way of uploading a file with its name with Retrofit 2, without any hack:
Define API interface:
Upload file like this:
This demonstrates only file uploading, you can also add other parameters in the same method with
@Part
annotation.Uploading Files using Retrofit is Quite Simple You need to build your api interface as
in the above code image is the key name so if you are using php you will write $_FILES['image']['tmp_name'] to get this. And filename="myfile.jpg" is the name of your file that is being sent with the request.
Now to upload the file you need a method that will give you the absolute path from the Uri.
Now you can use the below code to upload your file.
For more detailed explanation you can visit this Retrofit Upload File Tutorial.
I used Retrofit 2.0 for my register users, send multipart/form File image and text from register account
In my RegisterActivity, use an AsyncTask
And in my Register.java class is where use Retrofit with synchronous call
In the interface of RegisterService
For the Utilities parse ofr InputStream response