Retrofit Tutorial in Android – Part 2 POST Requests

Retrofit Tutorial - part 2 Post Requests
This article is a continuation for the Retrofit Tutorial in Android.

To make sure you follow up with the tutorial, I advise that you read my previous article Retrofit Tutorial in Android – Part 1 Introduction

So in the first part of the tutorial, we had an introduction to Retrofit library, discussing in brief about its powerful features, we started the tutorial with including the Retrofit library reference, creating the Retrofit Instance, preparing the Retrofit Interface that will match the API endpoints for the countries web service, and then we built our simple Regions/Countries app.

My main focus was to get you started with Retrofit and let you understand the concept behind using this library with its many easy to use features instead of using the OkHttp Library.

In this tutorial, I will show you how can you do a POST request using Retrofit.

So bear and focus with me so that you can get the best out of this tutorial.

Doing an HTTP POST Request

Usually, whenever you have a web service that is defined as POST, the intention behind it will be that this service will save/add data on the backend side, such as adding a user from a sign up form.

An HTTP POST request allows the consumer to apply a body to the request, a request body’s format can be defined by the Content-Type header.

In Retrofit, you don’t have to define this header specifically, because the Converter will do it for you. If you remember, in the previous article we used the GsonConverterFactory at Retrofit instantiation. This factory does the job on behalf of us.

So let me show you how can we do the POST Request using Retrofit.

Please note that I will not explain all parts of the code, as many steps have been explained in depth in the Part 1 of the Retrofit Tutorial in Android.

We will be doing our Post request to a public testing api reqres

Preparing the project and Retrofit-related setup

Start a new project, and import the needed retrofit libraries as the below:

Then create the retrofit interface, name it IUsersApi

Now, we will create a Post request, for this we will use the annotation @POST and the annotation @Body to pass the needed model to the API. See the below

Now, let’s create the User Model. Create a class with name User:

After that we need to define our Retrofit Instance. This has been explained in the previous tutorial.

Then we will extend the Application Class, to create our singleton Retrofit Interface.

Consuming the createUser Method

Now, we have the foundation to the Users API provided by reqres.in , we will consume the createUser method, to do a POST Request from the app.

Open activity_main.xml, and add 2 EditText widgets with 2 TextViews as Labels under each other and a Button. Make sure you change the activity_main layout to be LinearLayout.

Now open MainActivity.java, and get reference to the EditTexts and the button, and add the ClickListener to the Button.

Before we run the app , don’t forget to add the Internet Permission to your manifest file.

And in the <Application tag , add the name to be MainApplication

Running the app

Run the app, fill the Name and Job fields, and press Save User button. You should see the progress bar spinning underneath the button

Retrofit Tutorial post request process

And then, you should get the Toast Message telling you the name, job, id and TS for the created user. Where these values are turned back from the API, as the below screenshot.

Retrofit Tutorial post request success

I hope that was clear for doing a POST Request via Retrofit.

In the next tutorial I will explaining how can we pass request headers for the services that request them. We will access the Oxford Dictionaries API and do authenticated request call through passing header parameters: app_id and app_key

Stay tuned, and let me know if you like this article.

 

Bonus

Enjoy this wonderful rendition of an hour long Italian Baroque ‘concertos’ by the composer Giuseppe Sammartini

Leave a Reply