Retrofit ResponseBody

A look at the generic response from a Retrofit call

For a more complete and in-depth explanation, check out our complete Retrofit course

Download the starter project here

Github repository

Generic API response

ResponseBod is a generic api response. When you create a function in your interface that creates a call, that call can return a ResponseBody.

There are many reasons why you might want a response body. For instance you are not interested in the response body, so you don’t want that JSON or XML converted into an object in your application, you’re more interested in whether or not the request succeeded, and if it returns the proper headers.

Custom data format

You might also want to use it if you have a custom format for your data. When you want to simply retrieve what the server sent you and process it in a certain way, then you could simply get a ResponseBody and do your own processing in your application without relying on Retrofit.

Here are some examples of what you might except to use from a ResponseBody

responseBody.isSuccessful

For instance you can use the flag isSuccessful which will tell you whether the operation succeeded or not. You might want to just rely on that rather then parsing the object which might take some computational power.

responseBody.headers()

You might also be interested in just the headers.

responseBody.raw()

You might want to get the raw information sent by the server, so maybe just a text string to do your processing yourself in the app

responseBody().code()

You might also be interested in just the HTTP code that is returned.

All this information of course you could get from a body that was converted in an object in your code, but if you’re not interested in that processing you can just return a ResponseBody and get access to the information that you’re interested in.

For a more complete and in-depth explanation, check out our complete Retrofit course

Close Bitnami banner
Bitnami