Android Retrofit Post Request With Parameters JSON Example

Android Retrofit Post Request With Parameters JSON Example welcomes you.

You will learn to make http call using retrofit post request with parameters in this tutorial.

Several server calls like log in, registration, near by place etc. request need to have parameter with the URL.

For example, you want to fire login web service than you need to pass username and password along with the URL.

So here I will show you how to do this with retrofit POST request in android application.

Watch the Retrofit POST

Load the following video to see final outcome of this video.

Step 1. Initial Works

Our first first task is to make a new project in android studio with empty activity.

After this, in the AndroidManifest.xml file, add the following line.

Above line is the internet permission. We need this permission to use the internet of the android device.

Now in your build.gradle(Module: app) file, write the following line

  • First line will fetch the classes of the retrofit 2 library.
  • Second will enable us to use the scalar library in our project.

Step 2. Retrofit Interface

For using retrofit, we need to make one Interface.

Make a new JAVA file and give it a name like

Source code for is looking like below file

  • first line is defining a string variable. This variable includes the URL for login.
  • Consider @POST(“simplelogin.php”). Here, @POST is saying that retrofit will make POST request.
  • Inside getUserLogin() method, there are two string parameters which we need to pass with the web service URL.

Step 3. Showing Credentials

Prepare a new activity called “Hobby Activity.”

System will create two files : activity_hobby.xml and

Source code for activity_hobby.xml should look like the below

  • This file includes two text views. One will hold the value of user name or first name.
  • Second text view will get the value of hobby in string format.

Now in file, you should add the following source code

  • As we have added two text views in activity_hobby.xml , here we will get them using their ID.
  • Both these text views will get their values from two public string variables defined in the Main activity.

Step 4. Final and Main thoughts

Time to write two main files of this example : activity_main.xml and

In your activity_main.xml file, add the below code snippet

  • Above file have One text view, two edit texts and one button.
  • text view is just saying the company name.
  • I have added one edit text is for username and other one is for password.
  • When the user clicks the button, system will make the http call.

Now code structure for is looking like the below

Taking above code seriously

Let us see words of main activity in deep.

Consider the following code block

  • First line is making an object of edit text and second one is of Button.
  • Third line making two public static string variables. We have used these variables in hobby activity.

Now read the below code block

  • When the user clicks the button, compiler will execute the above code.
  • It will simply call the loginUser() method.

Following is the coding lines for  loginUser() method.

  • First two lines will declare the two string variables.
  • First one will get the string value from etUname edittext. It is the username entered by the user.
  • Second line will give the password of the user to the string variable from another edit text.
  • After this, compiler will create the object of the Retrofit class.
  • Here, .baseUrl(PostInterface.JSONURL) line is telling retrofit about the URL.
  • After this, compiler will create the object of the PostInterface and Call<String> classes.
  • Now, api.getUserLogin() method will get username and password as a parameter.
  • At this point, system have made the http call to the remote server. When the compiler get the response, it will call the onResponse() method.
  • Inside this onResponse() method, we will get the response in string format.
  • After this, compiler will call parseLoginData() method.

Following is the main source code for parseLoginData() method.

  • First of all, compiler will check the value of “status” field of the JSON response. If this value is “true” then it will parse a json array.
  • Compiler will parse the Json array using the “data” field from the string json response.
  • Now it will go through one for loop. Inside for loop, it will get the value of “name” and “hobby.”
  • It will store these values into public string variables called “firstName” and “hobby.”

After this, system will open the Hobby Activity.

Download Source Code for Android Retrofit POST Request with Parameters

Click the below github link to download android studio source code.

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *