This tutorial teaches you how to connect Android with PHP and MySql. We will get JSON data from a server to the client application.

We will create a server application using PHP which will get data from a MySQL database. Then we transform that data into a JSON object with the help of PHP. After that, we get JSON data from the Android client application. This is how we can connect Android with PHP and MySQL.

Steps to follow to connect Android with PHP

  1. Implement an interface class which will help to connect Android and the PHP server.
  2. Create the Main Activity.
  3. Perform the background processing using AsyncTask.
  4.  Create server sided code in PHP to connect to MySQL.


Let’s create a client/server interface class which is used to get data from the server. We use PHP to create the JSON object and we use JSON data in the mobile application.


First we connect Android with the PHP server. Then we send and retrieve data using the HTTPClient. The HTTPPost specifies that our request method is a post. The response is stored in HTTPResponse. The “‘URL” is the actual link where JSON is present. Then we use an inputstream to get data into bytes. We need to convert a byte stream to a character stream. After that, we build String with the help of StringBuilder.

Next, we create the main activity. We use AsyncTask to enable background processing. The connection between the Android client application and PHP server is network intensive so we use another thread to process the request instead of the main thread.

Server Sided Code

Create a JSON array using PHP and MySQL. Get data from the database table and transform it to a JSON array.


This is how we get JSON data from the server and how we connect Android with PHP and MySQL. Feel free to post if you have any errors in your application.