In this video, you'll learn how to send Java object in the request body to the server with Retrofit.
Tip: turn on subtitles to deal with my accent. A shortened transcript is also available below.
Read the full tutorial here:
Watch 20+ Retrofit videos in our playlist here:
Our book on Retrofit is also available on leanpub:
Future Studio is helping 5,000+ users daily to solve Android and Node.js problems with 320+ written tutorials and videos. We’re on a mission to provide new in-depth content every week.
Checkout 320+ technical in-depth tutorials:
Subscribe for two new videos every week:
Follow us on social media to get updates on new content:
In the first video in this series, we've learned what Retrofit is and how you can use it to request data from the server. If you haven't seen that video yet, you should watch it right now since it'll make it easier for you to understand the rest of this video.
In this video we'll turn things around and send data to the server. In just a few minutes you'll know how you can Java objects as your request body.
Let's quickly review GET, POST and PUT requests. If you're requesting data from a server, you're doing a GET request. In this video we want to send data to the server. This is usually done in a POST or PUT request. A POST request is if you're trying to add a new data item, for example if you're sending user object the server will create a new account. On the other hand a PUT request updates an existing data item. In the user example, this would be a profile update. It doesn't create a new user, it simply changes an existing one.
Today we'll implement an app view which we'll let the user create a new account. Thus, we're sending a new user object as a POST request to the server.
I've already prepared a view and the basic Retrofit setup. We need to describe the endpoint we want to talk to. The API developer gave us an example request in Postman, which is a pretty decent tool to test REST requests. Now this is everything we need to know to describe our endpoint in Retrofit. If we execute this we'll also see that we're getting the exact user object back, plus an ID.
Alright, now we need to describe that endpoint we've just seen as a Retrofit endpoint. We're creating a new account with the user object, so let's pass a user object. We're also getting a user object back, so depending on the context you want to use different classes for responses and requests, but in this case it's almost the same, so we're just going to use the same class. As always in Retrofit you've to wrap it into a Call class.
First, we know that this is a POST request, so let's add the @POST annotation. The final piece is to declare the user object as the request body. This is simply done by a @Body annotation. Retrofit will now create a request body out of the passed user object.
As you know from our Postman request, we've four properties here. Additionally, when we're getting the user back we'll be getting an integer. Since this is nullable, I'll be using the Integer class for that one. Finally, let's add a constructor without the ID, since when we're passing to the server we don't have an ID yet.
Once the user clicks on the "create account" button, we need to read in these properties, create a user object and then send it to the server. We can use the constructor we've just created.
I've already prepared a client for UserAPI. So what we now need to do is to get a call object for our request. And we can do that by simply calling client. and there is our new method. We can pass the user object we've just read in from the endpoint. We're getting back a Call object. Since we're working asynchronously, we'll use enqueue() and we're going to pass a callback.
Now if something went wrong, I'll just going to do a little Toast.
We know when we get the user object back from the server, it has the user ID. As you know from the last video, you can access the server response via response.body() and since we declared a getter ID method, we can use that here.
A nice thing about Retrofit, which I haven't said yet, is if an object property is null Retrofit will just ignore it. In our user class, the ID is null when we're sending it to the server. Since it is null, Retrofit will not even create a JSON property for it. It would just ignore it.