XML parsing in Retrofit Android - 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.

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);

How to send post request along with data in raw form in Retrofit?

I want to send a data in the above format in raw form of Body. The data type should be "JSON(application/json)" and the header should include "application/json" as Content-Type.
I tried using this way but is not working for me.
public interface AddData {
#POST("/Api/")
Call<String> postData(#Header("Content-Type") String content_type,#Body JSONObject body);
}
Use #Header as the annotations
#Headers("Content-Type: application/json")
#POST("/Api/")
Call<String> getUser(#Body JSONObject body);
Refer to https://github.com/square/retrofit/issues/1587

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

#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.

How to send #FormUrlEncoded and #Multipart request at the same time using Retrofit lib

I am writing a client-server Android application. I need to send a file created by user (photo) to server via POST request. The problem is, that when i try to send a file, i can't add a POST Field to my request. Maybe I'm wrong fundamentally, and this operation should be done another way?
#FormUrlEncoded
#Multipart
#POST("/answer/add-file")
Call<AbstractServerResponse> sendSingleFile(#Query("access-token") String accessToken,
#Query("w") String screenWidth,
#Field("answer_id") Integer answerId,
#Part("file") File fileToUpload);
When i try to send files in a multipart way only, i get an exception:
java.lang.IllegalStateException: JSON must start with an array or an object.
As I understand, this happends because the body (main part) of the request is empty.
Multipart requests are used when #Multipart is present on the method. Parts are declared using the #Part annotation.
------ from http://square.github.io/retrofit/
I'm using #Multipart in my project like this:
#POST("/{action}")//POST 多个文件
#Multipart
public void PostAPI(
#Path(value = "action", encode = false) String action,
#Part("path") TypedFile[] typedFiles,
#PartMap Map<String, String> params,
Callback<APIResponse> callback);
Maybe u can try this:
#Multipart
#POST("/answer/add-file")
Call<AbstractServerResponse> sendSingleFile(
#Query("access-token") String accessToken,
#Query("w") String screenWidth,
#Part("answer_id") Integer answerId,
#Part("file") File fileToUpload);
You cannot use both #FormUrlEncoded and #Multipart on a single method. An HTTP request can only have one Content-Type and both of those are content types.

How #PUT some value in webservice via Retrofit

I wanna send a list of integer with userName and password to WebService some thing like bellow request
UpdateDocumentState(List<int> documentIds, string userName, string password)
But I don't know How to do that ? Use #Post Or #Put ? use #Query Or #Field ? I googled but didn't find any good example or tutorial which explained these well. ( All tutorial I found was about #GET )
could anyone give me some piece of code , how to do that ?
About the use of #PUT or #POST I think you had to get this information from the WebService developers.
Anyway, here sample code for both of Retrofit annotations with or without Callback response.
#POST("your_endpoint")
void postObject(#Body Object object, Callback<Response> callback);
#PUT("/{path}")
String foo(#Path("path") String thePath);
EDIT:
Object is a custom class which represent the data you had to send to the WebService.
public class DataToSend {
public List<Int> myList;
public String username;
public String password;
}
For example when the #POST annotation declaration will be:
#POST
void postList(#Body DataToSend dataToSend, Callback<Response> callback);
and then you call the method using Retrofit service
yourService.postList(myDataToSend, postCallback);

Categories

Resources