Android Populate Spinner From JSON Array Web Service

Greetings with Android Populate Spinner From JSON Array and Web Service Tutorial Example.

You will learn how to populate an android spinner using a json web service and MySQL Database in this post.

To display json data in spinner in android, we need to create string arraylist using web service.

First of all, we will retrieve the JSON Data from MySQL using web service.

Then from this JSON data, we will parse the information in string format.

We will store this information in string arraylist and will use them to load the spinner.

See Spinner

So follow the below steps to load or set android spinner value from json array and web service and MySQL Database.

Step 1. JSON Data To Parse

Following is the json data that we will use to populate the spinner.

URL to the web service is:

Step 2. Using Internet

Our app will internet to call the web service.

For this, we need to write Internet Permissions in the AndroidManifest.xml file.

So the code for AndroidManifest.xml looks like the below.

Step 3. Drawable Tasks

We will make our spinner little bit attractive.

For this, we need one image as the dropdown icon.

Click the below link to download the image.

Download arrow Image

After downloading the image, add it to the res->drawable directory.

Now, we need to create a new XML file under res->drawable directory.

Give this file a name like spinner.xml and add the following source code in it.

Above file will draw borders around the spinner.

Color of the border will be pink.

You can change the color and width of the border by changing the below line.

Change the radius of the corners by editing the following

Dropdown icon can be changed with below code structure

Step 4. HttpRequest Class

Create a new JAVA class named

Copy the the following code snippet in file

Methods of above class will help to call the web service with our URL.

Step 5. Model Works

Make another JAVA class and give it a name

Write down the below code structure in file

Our JSON includes the information like name, country city and image url.

This model class have getter and setter method for this information types.

It helps to reduce complexity and data maintenance.

Step 6. Main Activity Works

Your activity_main.xml file should look like the below coding area

I have taken one spinner in this file.

Set the spinner.xml (created in step 3) file as the background of the spinner. It will modify the look and feel of the spinner.

Now in the file, you should write the following lines

Learn above file step by step

See the following source code

First line is the string that contains the URL to the web service.

Second line is just a integer.

Third one is the object of the ProgressDialog class which we will use to show the progress dialog while fetching the json data.

Fourth line is making an arraylist with the objects of the ModelData class.

Fifth one is making the string arraylist.

Sixth one is the object of the Spinner class.

In onCreate() method, compiler will call the loadJSON() method.

Code block for the loadJSON() method is as the below

First of all, compiler will show the loading dialog.

Then it will call the web service to fetch the JSON data.

After fetching the JSON, it will call the onTaskCompleted() method.

Code for onTaskCompleted() method is as the below

Compiler will populate the modelDataArrayList using parseInfo() method.

Then it will make one for loop, that will add the names of the players in the string arraylist (names).

After this, compiler will set the values of the spinner using this string arraylist (names).

below is the parsInfo() method’s code

Compiler will first check the status which is the object in the JSON response.

If the status is true then it will parse the JSON array called “data

This json array “data” is having the information.

So compiler will make one for loop to parse the information in every iteration.

Here, it will create the object of the ModelData calss and will assign the information to it.

Then this object is added in the arraylist and at last, it will return this arraylist.

Download Source Code For Android Populate Spinner From JSON

Download Spinner_JSON_Old 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 *