Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of Call<T> type with Retrofit POST

Tags:

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.

like image 624
Aaron Avatar asked Apr 10 '18 01:04

Aaron


People also ask

What is call in retrofit?

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 the use of retrofit?

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.

What is the advantage of retrofit?

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.


1 Answers

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

like image 109
Raja Jawahar Avatar answered Sep 28 '22 06:09

Raja Jawahar