How to upload photo in android retrofit 2? - android

I can't upload photo to server. java.lang.IllegalArgumentException: #Field parameters can only be used with form encoding. (parameter #1). Please help me, how to solve or any other suggestions. postman
#FormUrlEncoded
#Multipart
#POST("qq/api/xxxx")
Call<Custom> postCustom(#Field("Id") String Id,
#Part MultipartBody.Part file,
#Field("Status") String Status);

Change your request interface like this
#Multipart
#POST("qq/api/xxxx")
Call<Custom> postCustom(
#Part("Id") String Id,
#Part MultipartBody.Part file,
#Part("Status") String Status);
You cannot use both #FormUrlEncoded and #Multipart on a single method since an HTTP request can only have one Content-Type. #FormUrlEncoded and #Multipart are both content types.
From Jake Wharton
You can have to use FormUrlEncodedTypedOutput as a #Part argument for the form encoded part and build it up yourself. The annotation on the method is for the outermost encoding which in this case is multipart.
References
I have added also these issue reference which is really helpful for you to understanding what you are about to change in your http request method
https://github.com/square/retrofit/issues/662
https://github.com/square/retrofit/issues/1210

Related

Send optional field and multipart in retrofit

I want to send image , name and city for the server and all of these can be optional but i don't know how the interface will look like i found a solution with overloading the methods but it makes the code complicated
The http method is PUT also .
Use the annotation for Multipart. Create a Map of city and name to be used as PartMap. Pass the image as MultipartBody. Something like this:
#Multipart
#POST("")
Call<ReturnType> yourCall(#Url String url, #PartMap() Map<String, RequestBody> params, #Part MultipartBody.Part multipart, ...);

Multiple Parameter in REST-PUT with both #Path and #Body

REST-API PUT with both #Path and #Body as input parameters
I am trying to create an API PUT method which should use both #Path and #Body as input parameters but cannot find the exact example(sample code) to achieve that.
This is a sample for your requirement.
#PUT("/api/{username}")
void putAPICall(#Path("username") String username, #Body RequestBody params);

How to pass extra post data when using Multipart in Retrofit?

I'm Using the below code in Android, retrofit, to upload an image:
#Multipart
#POST("uploadimage")
Call<ImageUploadResponse> uploadImage(#PartMap Map<String, RequestBody> map);
But what if I need to send extra data, such as Image Description along with the request?
I Was not able to use #Field, that is i tried like this:
#Multipart
#FormUrlEncoded
#POST("uploadimage")
Call<ImageUploadResponse> uploadImage(#PartMapMap<String,RequestBody> map,
#Field("description")String desc);
I got an error stating that only one annotation is allowed. That is either #Multipart or #FormUrlEncoded.
You can use #Part instead of #Field
#Multipart
#POST("uploadimage")
Call<ImageUploadResponse> uploadImage(#PartMap Map<String, RequestBody> map, #Part("description") String description);

Retrofit: what is different between #Field and #Body

In some post request, I don't know when to use #Field, when to use #Body.
Like whats the difference between:
#POST("users/register")
Call<String> register(#Body RequestBody registerRequest);
and:
#POST("users/register")
Call<String> register(#Field String id, #Field String pass);
Can I use #Body instead of #Field, and reverse ? If not, why ? And how to know this case use #Body, other case use #Field ?
Can you please give me some case and explain, thank you.
#Body – Sends Java objects as request body.
#Field – send data as form-urlencoded. This requires a #FormUrlEncoded annotation attached with the method.
The #Field parameter works only with a POST. #Field requires a mandatory parameter. In cases when #Field is optional, we can use #Query instead and pass a null value.
Both are used for posting data only, but they have following difference -
The #Body annotation defines a single request body.
interface Foo {
#POST("/jayson")
FooResponse postJson(#Body FooRequest body);
}
That means if you are using #Body, it should be only parameter. It is helpful when you have already a JsonObject and you want to send it as it with you api call.
Another way is, you can send data using #Field and send the Place object as a JSON string.
#POST("/post/addphoto/")
public void addImage(#Field("image_url") String url, #Field("caption") String caption, #Field("google_place_id") String placeId, #Field("facebook_place") String place, Callback<UploadCallBack> response);
Hope it will help... :-)

Using #FieldMap and #Part in single request in Retrofit2 gets java.lang.IllegalArgumentException: Only one encoding annotation is allowed.for method

This might seem similar to earlier questions but none actually answers my question.
I need to Post multiple fields and multiple images in one request using retrofit2 and i'm getting this error
java.lang.IllegalArgumentException: Only one encoding annotation is allowed.for method xxx
i'm using
#Multipart
#FormUrlEncoded
since #Field requires #FormUrlEncoded and #Part requires #Multipart.
the more logical thing to do is to remove the #FormUrlEncoded annotation, but how do i go from there.
Now the question is how do i go about the task to achieve sending my post in a single request.
here's the interface
#Multipart
#FormUrlEncoded
#POST("upload")
Call<ResponseBody> uploadPost(#FieldMap Map<String, String> map,
#Part MultipartBody.Part image1,
#Part MultipartBody.Part image2,
#Part MultipartBody.Part image3);
#Multipart
#POST("upload")
Call<ResponseBody> uploadPost(
#PartMap() Map<String, RequestBody> descriptions,
#Part List<MultipartBody.Part> images);
use this interface.

Categories

Resources