pass two objects in POST retrofit 2.1.0 service - android

How to post multiple objects in POST retrofit service?
I POST single object and it works fine but now i want to send two objects in POST retrofit web service
Below code for single POST object that works fine
#POST("Device/AddDevice")
Call<String> addDevice(#Body Device value);
I code for multiple objects but it give error
#POST("Device/AddDevice")
Call<String> addDevice(#Body Device deviceValue , #Body User userValue);

Hi Looks like a duplicate. But here the answer from this question
#Multipart
#POST("Device/AddDevice")
void addDevice(#Part("deviceValue") Device coordinates,
#Part("userValue") User maxDistance,
Callback callback);
For more documentation and examples on retrofit have a look at the follwoing page: https://futurestud.io/tutorials/retrofit-getting-started-and-android-client

Related

Sending Array as Param in Retrofit Get Request A

I am trying to send an array as parameter in GET request in Retrofit
Eg: prod = [1,2,3,4]
Tried almost every solution present on Stack OverFlow, GitHub
Non of them worked
Can someone explain how to make an array and send get request in RetroFit
Thankyou in advance!
You can try like this
#GET(".....")
Call<ResponseBody> getSomthingApi(#Query("prod") ArrayList<String> values);

integrating Delete Method API with retrofit 2 android with multiple parameters

I am using retrofit 2 for HTTP request in android. The API has delete method implemented and expect 3 parameters I am able to integrate with 1 parameter but could not be able to perform with 3 parameters. While running it on postman the API response is ok
I have tired with one parameter and it is working fine for me
Here is the sample for multiple parameters on the DELETE method:
#Headers("Accept: application/json")
#DELETE("/users/{x}/data/{y}/{id}")
Call<Response> deleteData(
#Path("x") String x,
#Path("y") String y,
#Path("id") String id
);

Retrofit 1.9 doesn't post data correctly to server side

Hello i have a problem with post method on retrofit 1.9.
I'm sending a data in Pojo as a #Body and it's return that i didn't send anything.
Here is the code of serviceInterface :
#POST(Constants.API_MAKE_ORDERS)
void makeOffers(#Body MarketPlaceRequest marketPlaceRequest,
Callback callback);
[
Thx in advance.
While debuging code,where you can see filled request
And here is a log of retrofit in android monitor]

Where in Retrofit build the full JSON before sending it?

I am using retrofit an get Bad Request , I would want to know if there is a place in this library where builds the full JSON in string format before sending it.
If it's about inspecting the JSON at runtime for debugging purposes, you can call setLogLevel(LogLevel.FULL) on your RestAdapter.Builder.
FULL logs the headers, body and metadata for both requests and responses to logcat.
new String(((TypedByteArray) request.getBody()).getBytes());
In order to build a JSON formatted body, create an object with a class whose properties are the same that you want to send to the server. The GSON Library set up (or whichever library you are using) with the RestAdapter should send the request with the body in JSON format.
Also ensure that the call is #POST annotated and the parameter annotd with #Body Below is an example:
#POST("/login")
User login(#Body LoginUser loginUser);

POST request with Android Annotation REST Service

I am using Android Annotation in my project and trying to send POST request through following code, however there is something wrong in following code as I am not getting response as expected:
#Rest(rootUrl = "http://xyz.com", converters = {GsonHttpMessageConverter.class})
public interface A {
#Post("/authenticate/email/")
public Object attemptLogin(Map data);
}
Where data is (key, value) pair. Is there anything I am missing perhaps Do I have to set request-header or data should not be JSON?
I found the solution from Rest client using Android-Annotations.
Like the GET requests, it is extremely simple to send POST requests using Android-Annotations. One difference is that you need to define the parameters that you are going to send as a custom class (e.g. Event class in the example below) or if you want to control this dynamically, then a Map (e.g. a MultiValueMap). The url for the request can still be constructed in a similar fashion using the variables enclosed inside {...} and the response can be handled similarly as in GET requests.
#Post("/events")
void addEvent(Event event);
#Post("/events/{id}")
void addEventById(Event event, long id);
#Post("/authenticate/event/")
Object authenticateEventData(MultiValueMap data);

Categories

Resources