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 …

filter json android example

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.

Download Laptop.SQL

Database

  • 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

  • Next, create a php file named DbConnect.php

  • Next, create a php file named DbOperation.php

  • Finally, create a php file named getData.php

  • 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.

  • 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.

  • Here’s the MainActivity.java. In here, the data from the server is fetched as the getData.php is implemented  through the method getLaptops() and the stored into SharedPreferences as String.

  • Now, create a layout resource file named list_layout.xml using the following code. It will contain the layout of the list view in activity_unsorted.xml, activity_sortedbybrand.xml and so on.

  • Now, create a class named Laptop using the following code.

  • Now, create a class named LaptopAdapter using the following code. It will serve as the adapter to the listView.

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.

  • Here’s the UnsortedActivity.java. In here, the String containing the data is fetched through SharedPreferences and then manipulated through JSON.

  • Next, create another Empty Activity named Sortedbybrand and then configure its xml and java parts using the following codes:-
  • Here’s the activity_sortedbybrand.xml. It simply contains the ListView in which the list of laptops will be displayed.

  • Here’s the Sortedbybrand.java. In here, the String containing the data will be fetched through SharedPreferences and then sorted through JSON by brand.

  • Similarly, create another Empty Activity named Sortedbyprice and configure the xml and java parts using the  following codes.
  • Here’s the activity_sortedbyprice. It again contains the ListView as in activity_soredbybrand.

  • Here’s the Sortedbyprice.java. In here, the String containing the data will be fetched through SharedPreferences and then sorted through JSON by price.

  • Similarly, create another Empty Activity named Filterresults and configure the xml and java parts using the  following codes.
  • Here’s the activity_filterresults.xml. It again contains the List view.

  • Here’s the Filterresults.java. In here, the String containing the data will be fetched through SharedPreferences and then filtered through JSON by price being less than 40000.

  • You are done with the activities here.
  • Finally, just add the internet permission to your AndroidManifest.xml and your Filter JSON app is complete.

  • That’s it:). Run the app the wait to see the Toast saying data fetched Successfully.
filter json data example
Filter JSON
filter json data
Filter JSON
  • If you are facing troubles then download my source code for this filter JSON project from below.

[sociallocker id=1372] Filter JSON Source Download [/sociallocker]

If you ran into some problems regarding this filter JSON Data tutorial, let me know in the comments section. I will try to sortout your problems ASAP.

Hello, I am Manish Kumar. I am a B.Tech Student at NIT Jamshedpur. Fortunately, I find myself quite passionate about Computers and Technology. Android is my most recent Crush.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x