Android PHP MySQL – Android MySQL RecyclerView – ServerSide Search/Filter Tutorial

This is an Android MySQL Search tutorial. We are performing search on the server side against our MySQL database. Our widget is RecylerView.


The world we live in is indeed large.The data we have is in astonishing quantity.Even programming,atleast in most cases involve manipulating or reading some data.

This tutorial is no exception.Our aim is to see how to filter data from our MySQL database.Then we show our results in Realtime. We are performing a server-side search.Generally speaking,this is much faster than filtering at the client.Its faster than say,downloading your data to the device,then filling some sort of arraylist.

Then filtering the arraylist.What if am having 100,10000,1 million records etc.You get the point. You’ll hog the users bandwidth.And it takes time.Writing optimized java code to search can be tricky itself.Now we can save ourselves some trouble.Perform the search on the big beefy servers.

Then download results.SQL is normally optimized for search/select performance.And we trust it better than ourselves. Anyway we use class,a subclass of


Android MySQL RecyclerView Search Filter

Android MySQL RecyclerView Search Filter

Android MySQL RecyclerView Search Filter

Android MySQL RecyclerView Search Filter


  • Basically,its a URLConnection for the web.HTTP,remember.
  • Its used to send and receive data over the web.Clients to server and vice versa.
  • Input and Output like we do in this tutorial.Using POST request method.We make a HTTP POST request.
  • HTTP POST is supports both input and output of data.And basically that’s what we need here as we are searching.
  • While searching,you send a query via outputstream,then receive an inputstream through which you read query results.

Read about HttpURLConnection here.

Sending Data

  • First the HttpUrlConnetions’s setDoOutput(true) to allow connection.
  • HTTP POST allows sending data.

Receiving Response

  • First the HttpUrlConnetions’s setDoInput(true) to allow connection.
  • HTTP POST allows receiving data.

PHP Code



$con=mysqli_connect($host,$username,$pwd,$db) or die('Unable to connect');
    echo "Failed to Connect to Database ".mysqli_connect_error();

$sql="SELECT * FROM soccerTB WHERE Name LIKE '%$name%'";
    echo('Not Found ');


Java Code

Our Connector Class
  • Has a static connect method that takes a URL Address string and returns a HttpURLConnection object.
  • Simply establishes connection to our server.
  • We set connction properties like Request method which is “POST”,as we are making a HTTP POST request.
package com.tutorials.hp.recyclermysqlserver_sidesearch.MySQL;


public class Connector {

    public static HttpURLConnection connect(String urlAddress)
        try {
            URL url=new URL(urlAddress);
            HttpURLConnection con= (HttpURLConnection) url.openConnection();

            //SET PROPERTIES

            return con;

        } catch (MalformedURLException e) {
        } catch (IOException e) {

        return null;


Our DataPackager Class

  • Basically,we package our data here for sending.
  • Obviously in this case our data is our query string
  • First we add them to a JSON Object.
  • Then we encode them into UTF-8 format using URLEncorder class.
  • Then we return it as a string ready to be written over the network.
package com.tutorials.hp.recyclermysqlserver_sidesearch.MySQL;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

public class DataPackager {

    String query;

    public DataPackager(String query) {
        this.query = query;

    public String packData()
        JSONObject jo=new JSONObject();
        StringBuffer queryString=new StringBuffer();

        try {

            Boolean firstValue=true;

            Iterator it=jo.keys();

                String value=jo.get(key).toString();

                }else {


            }while (it.hasNext());

            return queryString.toString();

        } catch (JSONException e) {
        } catch (UnsupportedEncodingException e) {

        return null;

Section 3 : Our SenderReceiver Class

  • Yes,its our SenderReceiver class.To send us our query.To receive a response.
  • We send and receive our data in a background thread using AsyncTask,the super class of this SenderReceiver class.
  • While sending query or receiving results in background,we show our Progressdialog,starting it in onPreExcecute() and dismissing immediately onPostExecute() is called.
  • We establish an outputStream,write to it using OutputStreamWriter.
  • Thats how we send.
  • The OutputStreamWriter instance,we pass toBufferedWriter instance.
  • The bufferedWriter instance writes our data.
  • We then read the server response using bufferedreader instance.

Section 4 : Our Parser

  • Well we shall receive a JSON string
  • And it needs to be parsed.So we parse it here.
  • This may be time consuming hence blocking,depending on your data size.
  • So we do it in background thread.Once more using AsyncTask.
  • We don’t use any thirdparty library in this tutorial.Not even when parsing.
  • JSONObject and JSONArray are sufficient for us.So we use them.
  • Thereafter,we fill an arraylist.And pass it to our MyAdapter class.
  • So that it can be bound to RecyclerView.

Our Recycler Package

This is the package where we deal with RecyclerView stuff. It contains:

(a). Our MyHolder
  • Our ViewHolder class
  • Derives from RecyclerView.ViewHolder
  • We only need one TextView,so this is the only view we shall be holding.
(b). Our MyAdapter
  • Where we bind our views to our dataset.
  • We receive a Context object as well as an arraylist.The latter acts as our dataset.
  • So we first inflate our model layout using LayoutInflater class.
Our MainActivity
  • Launcher activity.
  • Initialize UI like SearchView.
  • Handle SearchView’s onQueryTextChangeListener();
  • Starts the sender Asynctask on button click,passing on context, urladdress, query string as well as recyclerview.

Our Layouts

(a). activity_main.xml
(b). content_main.xml
  • Our nice layout

Section 7 : Reminders

  • Remember to add permission for internet in your manifest file.
  • Under your tag