Filter JSON Data in Android Application using PHP and MySQL

Hi there, here I bring you this filter JSON lesson in which you can learn how to fetch data from the server database and then either sort it or filter it through JSON which would be displayed in the form of ListView. In this filter JSON project as an example, we’ll be creating a database of laptops on the server, fetch the whole lot of data and then sort or filter JSON received from the server. I’ll begin with the server side tasks. So let’s get started.

Setting the server for the project

First of all we need to set up the database on the server as I have already in the image below. Just download the db below named laptop.sql and import it to your PhpMyAdmin.

Now when your db is set up. Lets write Php scripts to fetch the data from the database.

Creating PHP Scripts

First create a php file named Constants.php

Constants.php

1

2

3

4

5

6

7

8

9

10

11

12

<?php

/**

* Created by PhpStorm.

* User: Manish

* Date: 9/3/2016

* Time: 5:33 PM

*/

define('DB_HOST','Your Host name');

define('DB_USERNAME','Your database Username');

define('DB_PASSWORD','Your database Password');

define('DB_NAME','Your database name');

Next, create a php file named DbConnect.php

DbConnect.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<?php

/**

* Created by PhpStorm.

* User: Manish

* Date: 9/3/2016

* Time: 5:36 PM

*/

classDbConnect

{

private$con;

function__construct()

{

}

functionconnect(){

include'Constants.php';

$this->con=newmysqli(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME);

if(mysqli_connect_errno()){

echo'Failed to connect to mysql'.mysqli_connect_errno();

}

return$this->con;

}

}

Next, create a php file named DbOperation.php

DbOperation.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<?php

/**

* Created by PhpStorm.

* User: Manish

* Date: 9/3/2016

* Time: 5:40 PM

*/

classDbOperation

{

private$con;

function__construct()

{

require_once'DbConnect.php';

$db=newDbConnect();

$this->con=$db->connect();

}

publicfunctiongetLaptops(){

$stmt=$this->con->prepare("SELECT * FROM laptop ");

$stmt->execute();

$result=$stmt->get_result();

return$result;

}

}

Finally, create a php file named getData.php

getData.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

<?php

/**

* Created by PhpStorm.

* User: Manish

* Date: 9/3/2016

* Time: 9:00 PM

*/

require'DbOperation.php';

$response=array();

$db=newDbOperation();

$result=$db->getLaptops();

$response['laptops']=array();

while($row=mysqli_fetch_array($result)){

$temp=array();

$temp['id']=$row['id'];

$temp['modelname']=$row['modelname'];

$temp['ram']=$row['ram'];

$temp['os']=$row['os'];

$temp['price']=$row['price'];

$temp['screensize']=$row['screensize'];

$temp['brand']=$row['brand'];

array_push($response['laptops'],$temp);

}

echo json_encode($response);

If everything is fine you will see the data in JSON format, when you will run this script. If you can also skip this part if you want to use my database. Below is my live URL that you can use in this Filter JSON Tutorial.

http://internetfaqs.net/laptops/getData.php

Now let’s begin the android part.

Creating an Android Project for our Filter JSON App

Create a new Android project named JSONFilterApp. As your gradle is built and files are loaded, add the following dependency to your app level gradle. This will allow us to implement the php script in android.

VolleyDependency

1

compile'com.android.volley:volley:1.0.0'

As usual, we shall begin with configuring the activity_main.xml and MainActivity.java. Add the following codes into the respective files.

Here’s the activity_main.xml. It consists of four buttons leading to different activities. These activities will display the data in sorted and filtered manners.

Creating different activites for the Results

Now, create a new Empty Activity named UnsortedActivity which will display the unsorted list of laptops with their details. Now, configure the xml and java parts of this activity using the following codes.

Here’s the activity_unsorted.xml. It contains the ListView which will display the unsorted List of Laptops.

The main JSON Array you have mentioned must have a name .Let’s say its json1. Now json1 contains two json objects among which one is simple json object and another one is json array (worksheets) in the form of json obejct.

Suppose you received whole of this data in response. Just parse it using Json object in the following way:-
JSONObject js = new JSONObject(response);
JSONArray jsonArray = js.getJSONArray(“json1”);
Now you have your two json objects in jsonArray out of which one is a json obejct and another is itself an JSON array.
In the code below you can fetch you jsonarray called worksheet.

Hi Manish and Bilal,
Is it really a applying filer on activity, I was thinking this can be done using PreferenceActivity.
I need to implement filter like flipkart or olx in my application. I have searched a lot on web but did not found any desire result.
Suggest me, how can achieve this functionality please or post yourself a tutorial like flipkart or olx filter the json data as soon as possible.
Thanks in advance.

Definetly, the json data is getting filtered in different activities. As you run the project, on the home screen, look for the parameter on whose name the button’name is. Upon the button tap, it takes you to an activity where the data is filtered according to the parameter on the button.

// Close entire connection
$conn-> close();
where to put this method?
when i put this in my dboperation.php in getlaptops function,i get error syntax error, unexpected ‘users’ (T_STRING).What to do to correct this error?

Hi,
Thank you to share your work. I tried to do the same but Android Studio 3.1 seems not work properly.
It returns : Could not find method compile() for arguments [com.android.volley:volley:1.0.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy

About Simplified Coding

Simplified Coding is a blog for all the students learning programming. We are providing various tutorials related to programming and application development. You can get various nice and simplified tutorials related to programming, app development, graphics designing and animation. We are trying to make these things simplified and entertaining. We are writing text tutorial and creating video and visual tutorials as well. You can check about the admin of the blog here and check out our sitemap