How to use #Body with #Field and #FormUrlEncoded? - android

#FormUrlEncoded
#POST("/api/post")
Call<Response> createPost(
#Header("auth") String auth,
#Field("id") String id,
#Field("title") String title,
#Body ContentData content);
By using this code am getting error saying "#Body parameters cannot be used with form or multi-part encoding.". What should i do now ? I tried to send Object as String that also failed.

If you are sending form-data use Field for your parameters:
#Header("auth: YOUR_AUTH")
#FormUrlEncoded
#POST("/api/post")
Call<Response> createPost(
#Field("id") String id,
#Field("title") String title
);
else if you are using other than form-data like application/json Send your body by #Body:
#Header("auth: YOUR_AUTH")
#POST("/api/post")
Call<Response> createPost(#Body ContentData content);
This all depends upon your requirements.

Related

How to use two fields in body for methos put in retrofit

I have two strings that I should set them in body for my put request. How can do it with retrofit?
#PUT("/user-management/Account/activate")
#FormUrlEncoded
#Headers({ "Content-Type: application/json"})
Call<Verification> activation(#Part("code") String code , #Part("token") String token);
You may try similar code I posted below: For detailed code please post your context or a part of your code.
#Multipart
#PUT("user/photo")
Call<User> updateUser(#Part("photo") RequestBody photo, #Part("description") RequestBody description);
You can pass multiple Strings in Body like this:
Create a Class
public class Verification
{
public String code;
public String token;
}
Set the data to object
Verification loginCredentials = new Verification();
loginCredentials.code= "12345;
loginCredentials.token= "54321";
Call your api
#PUT("/user-management/Account/activate")
Call<Verification> activation(#Body Verificationcredentials);

Sending form-data with retrofit2

I have an endpoint where I PUT some data, where the Content-Type should be form-data.
I'm using
#FormUrlEncoded
#PUT("/api/v1/clients/profile/all_details")
Call<ResponseBody> postUserProfile(#FieldMap Map<String, String> userParams);
but while sending the request, the form-data is encoded like
food_allergy=%5Beggs%2C%20milk%2C%20nuts%2C%20none%5D&diet_prefer=Non%20Vegetarian&age=25&exercise_level=Moderate&email=Email&name=Veeresh&height=175&prompt-qgreet3=I%27m%20ready%21&gender=Female&health_condition=%5Bdiabetes%2C%20PCOD%5D&weight=69
How do I remove the encoding?
I tried this blog
https://futurestud.io/tutorials/retrofit-send-data-form-urlencoded-using-fieldmap
You can first of all replace all the % by using
string.replace("%","");
And do the rest the same way.
If you don't want to encode the form data then you can try like this
#PUT("/api/v1/clients/profile/all_details")
Call<ResponseBody> postUserProfile(#QueryMap Map<String, String> userParams);
EDIT 1
#Multipart
#PUT("/api/v1/clients/profile/all_details")
Call<ResponseBody> postUserProfile(#Part(username) RequestBody username, #Part(password) RequestBody password);
And here is how to get requestBody from String.
String username = "username";
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), username);
String password = "password";
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), password);
#FieldMap has an element called encoded which is false by default.
If you set it to true, you're saying my data is already encoded and I don't want Retrofit to handle the encoding.
#FieldMap(encoded = true) Map<String, String> userParams

how to send Image file with Retrofit(#Fields)

Here I m using #Fields data with #FormUrlEncoded But I have to use both in same API #Part("user_image") RequestBody file with #Multipart. How does it possible? Thanks in advance.
#FormUrlEncoded
#POST("/datingapp/index.php/Webservice")
Call<Result> signupUser(#Field("user_name") String name,
#Field("age") String age,
#Field("work") String work,
#Field("home_town") String home_town,
#Field("gender") String gender,
#Field("interest") String interest,
#Field("study") String study,
#Field("email") String email,
#Field("password") String password,
#Field("device_id") String device_id,
#Field("device_type") String device_type,
#Part("user_image") RequestBody file,
#Field("signup") String signup);
Http protocol not allow 2 Content-Type in the same request. So you have to choose :
application/x-www-form-urlencoded
multipart/form-data
You use application/x-www-form-urlencoded by using annotation #FormUrlEncoded in order to send image you have to transform the whole file into text (e.g. base64).
A better approach would be use multipart/form-data by describing your request like that :
#Multipart
#POST("/datingapp/index.php/Webservice")
Call<Result> signupUser(#Part("user_name") String name,
#Part("age") String age,
#Part("work") String work,
#Part("home_town") String home_town,
#Part("gender") String gender,
#Part("interest") String interest,
#Part("study") String study,
#Part("email") String email,
#Part("password") String password,
#Part("device_id") String device_id,
#Part("device_type") String device_type,
#Part("user_image") RequestBody file,
#Part("signup") String signup);
#Multipart
#POST("/datingapp/index.php/Webservice")
Call<Result> signupUser(#PartMap Map<String,String> queryMap,
#Part("user_image") RequestBody file);
Here the #PartMap contains the required other parameters which is nothing but a HashMap containing key and values e.g,
LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();
map.put("user_name",username);
like above and so on.
Make api call like this :
#POST("/datingapp/index.php/Webservice")
#FormUrlEncoded
#Multipart
Call<Result> signupUser(#FieldMap LinkedHashMap<String, String> data,#Part RequestBody file);
and pass your data is the form of key and value in LinkedHashMap like this
LinkedHashMap<String, String> data = new LinkedHashMap<>();
data.put("user_name", user_name);
data.put("age", age);
data.put("work", work);
data.put("work", work);
data.put("gender", gender); and so on ....
for getting image in Multiparts :
RequestBody file= RequestBody.create(MediaType.parse("image/jpeg"), file);
final call to hit the api :
Call<Result> call = apiService.signupUser(data ,file);
Hope this works :)

Retrofit 2 - how to post image using retrofit of form data

How to post this body type form-data using retrofit. I tried with #Part but image is not getting save on server. Any idea where I'm making a mistake?
#Multipart
#POST("/saveData")
Call<MyResponse> saveImage(
#Part("empsno") String empsno,
#Part("lrSno") String lrsno,
#Part("deliveryDate") String deliverydate,
#Part("deliveryTime") String delvrytime,
#Part("uploadFile") String upldfile,
#Part("remarks") String remark,
#Part("receivedBy") String recvdby,
#Part("place") String place,
#Part("ipAddress") String ip,
#Part MultipartBody.Part images
);
By using form data I'm successfully able to post data:
I made changes String to ResponseBody and now everything working fine
#Multipart
#POST("/saveData")
Call<MyResponse> saveImage(
#Part("empsno") ResponseBody empsno,
#Part("lrSno") ResponseBody lrsno,
#Part("deliveryDate") ResponseBody deliverydate,
#Part("deliveryTime") ResponseBody delvrytime,
#Part("uploadFile") ResponseBody upldfile,
#Part("remarks") ResponseBody remark,
#Part("receivedBy") ResponseBody recvdby,
#Part("place") ResponseBody place,
#Part("ipAddress") ResponseBody ip,
#Part MultipartBody.Part images
);

XML parsing in Retrofit Android

I am new to Retrofit and trying to parse xml in it. I am posting my code what I have try so far.
#FormUrlEncoded
#POST
Call<String> login(
#Url String url,
#Header("Authorization") String token,
#Field("mobilenumber") String mobile);
Output:
<Login>
<ResponseCode>1001</ResponseCode>
<ResponseMessage>Something went wrong.Please try again</ResponseMessage>
<ReferenceId>135745</ReferenceId>
</Login>
I don't know how to specify Object and add annotations to it according to the structure of the xml file.

Categories

Resources