Android Retrofit GET Request With Parameters Multiple QueryMap

Notice on Android Retrofit GET Request With Parameters example.

You will learn how to pass parameters in URL in retrofit GET request to remote server.

Server will reply us the JSON data in string format. We will pass retrofit multiple and dynamic query parameters along with the URL.

Watch GET retrofit Parameters With Hashmap

Step 1. Required Basic Tasks

Make a new project in android studio. Select Empty activity as the default template.

We need to make two basic work.

First of all, add the following line in AndroidManifest.xml file.

Now in your build.gradle(Module: app) file, write the below lines

  • First line will enable us to use the retrofit classes to make http call.
  • Second one will fetch the scalar library. This library will convert the JSON response in string format.

Step 2. Interface For URL

Let us make required interface. For this, create a new Java class and name it as GetInterface.java

GetInterface.java should have the following source code

  • First line is the string variable called JSONGETURL. This variable includes the URL which we need to fire.
  • @GET(“loginGETrequest.php”) is telling the compiler to make GET request to loginGETrequest.php file.
  • We will call getUserLogin() method to make the http call. It has hashmap (QueryMap) in it’s first parameter.
  • This hashmap includes the parameters which we need to pass with the URL.

Step 3. Showcase activity

Server will give us information like name, hobby, username, password etc information when we successfully get logged in.

In this activity we will show two information like name and hobby in text views.

So make a new activity and give it a name like NameActivity.java and activity_name.xml

Code block for activity_name.xml is as the below

  • As already mentioned, this file includes two text views.

Now in NameActivity.java file, write the following code lines

  • For writing name and hobby in the text views, I am using two variables which are defined in the Main Activity.
  • Access specifier for these variables are public static, so we can use them here in name activity.

Step 4. Final Attack

Now last thing is to write two main files : activity_main.xml and MainActivity.java

Code structure for activity_main.xml file is looking like the following

  • Several UI widgets are there in the above file. One text view, two edit texts and one button.
  • Text view have stationary text which you can replace with company name or logo.
  • One edit text will get the value of username and other one will get password from the user.
  • When the user clicks the button, retrofit will hit the GET call to the URL.

In the MainActivity.java , write down the below source code

Care about Main

Above class includes the important logic so let us understand it carefully.

First of all, see the below code snippet

  • First line is making the objects of Edit text class. Second is giving the button element.
  • Last line is creating two public static string variables. We have already seen their usages in the previous step.

Consider following code

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

Source code block for  loginUser() method is as the below

  • In this method, we will create two string variables first. (username and password )
  • These variables will get their values from two edit texts.
  • After this, compiler will create the object of the Retrofit class.

Here, .baseUrl(GetInterface.JSONGETURL) method will providing URL to the Retrofit object.

  • Now we are creating a hashmap. Here, you need to write parameters which we want to pass along with the URL.
  • In this login service, we are passing two parameters : username and password, so that , we will create two key value pair for hashmap.
  • You can add any number of parameters in this hashmap so it is working as dynamic json request.
  • Then we will create the object of Call<string> class.Here, we are providing this hashmap (parameters) to the URL.
  • Finally, call.enqueue() will fire the http call. When the server sends the response, compiler will call onResponse() method.

In this method, compiler will first convert the response in string format. Then it will call the parseLoginData() method.

parseLoginData() method holds the following code block

  • This methods takes the JSON response in string format as a parameter.
  • Now inside this method, compiler will first parse the json object. Then it will check the “status” field’s value. If it is true then it will parse one json array using “data” field from the response
  • After this, it will create one for loop and will fetch the value of name and hobby.

It will store these values into the public static string variables.

Download code for Android Retrofit GET Request With Parameters

Click this github link to download 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 *