New to http requests and java and retrofit so this is probably a basic question but...
I've managed to setup successful http calls to a web service, but was a little confused on one part, mostly based on what I've seen from tutorials/examples.
I've seen a number of examples with something like this:
@POST("/api")
Call<Foo> savePost(@Body Foo foo);
My understanding is that the argument 'foo' will be converted to json by the chosen converter and passed as the body of the http request. What I don't understand is why the response body type for Call is also Foo? Is the assumption here that the response will also be deserialized into a Foo object? Is it common practice to have a response that matches the body you're sending? I get using that for something like a GET, but don't really follow why you would expect a response like that for a POST.
An invocation of a Retrofit method that sends a request to a webserver and returns a response. Each call yields its own HTTP request and response pair. Use clone() to make multiple calls with the same parameters to the same webserver; this may be used to implement polling or to retry a failed call.
What is Retrofit. Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.
Advantages of retrofitIt is easy to use and understand. It supports request cancellation. It supports post requests and multipart uploads. It supports both synchronous and asynchronous network requests.
First let me explain what is call in Retrofit
It is a Retrofit Interface, can be called synchronously through execute or asynchronously through enqueue. In either case the it can be canceled at any time with cancel.
void enqueue(Callback callback);
Asynchronously send the request and notify
Response execute() throws IOException;
Synchronously send the request and return its response.
In the above example, the Response you will get from the savePost is also Foo.It will deserialise to Foo object.
Its depends on usecase, wheather you have to get the Foo object or not, If you dont want that you can use ResponseBody instead of Foo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With