What is responsebody in retrofit?

Viewed 367

I am new to retrofit and I know to send serialized objects to and from the server.

Earlier I used to create java objects and use gson for converting.

What is ResponseBody used for?

What does the below text mean?

A one-shot stream from the origin server to the client application with the raw bytes of the * response body. Each response body is supported by an active connection to the webserver. This * imposes both obligations and limits on the client application.

@Headers("Authorization: Key=$SERVER_KEY", "Content-Type:$CONTENT_TYPE")
    @POST("fcm/send")
    suspend fun postNotification(@Body notification: NotificationData): Response<ResponseBody>

The above code is what I am trying to understand

1 Answers

This actually is a good question.

The documentation says it very clearly:

This class may be used to stream very large responses. For example, it is possible to use this class to read a response that is larger than the entire memory allocated to the current process. It can even stream a response larger than the total storage on the current device, which is a common requirement for video streaming applications.

Because this class does not buffer the full response in memory, the application may not re-read the bytes of the response. Use this one shot to read the entire response into memory with bytes or string. Or stream the response with either source, byteStream, or charStream.

In resume, is a model class used for big responses that have to be cut in little pieces of data, as documentation says used for example for video streaming applications.

It's like a socket, an open connection that don't download all the data directly to the device to don't over consume the memory, you can split this information.

Don't forget to close the Response after using it:

Response.body().close()

Link to documentation: https://square.github.io/okhttp/4.x/okhttp/okhttp3/-response-body/

Related