How to connect android php mysql – generic asynctask

[updated on September 27, 2017]

One question: Getting Tired of Recreating AsyncTask Classes All Over Again?

Before we get started, let me say welcome to KosalGeek. I am glad you came!

OK, I get it. You came because you are like me; you are tired of creating tons of classes (mostly private classes) of the AsyncTask class all over an over again.

If you already know the basic and want to grab the code, you can jump to Reusable Generic Classes.

(You can watch video at about How to Connect Android with PHP, MySQL on YouTube, or you just watch it down below)

MOTIVATION

If you don’t know what I am talking, let me explain a bit. Basically, in Android when you want to connect to an external source, you use AsyncTask. Since Android 3.0 API 11 (Honeycombs), you are no longer allowed to use the main thread to do a long process; instead, you need the AsyncTask to work in a background thread. You can use Handler but it is a different approach which is not covered in this page.

Here, we use it to connect to a web service to retrieve data in a JSON or XML format (the latter is not common) from a database server (e.g. MySQL), and to send data (HttpPost, HttpGet) to a web service. For example, if you want to let your client log in from your Android app, then there are data sending back and forth. See the illustration below of how it works:

kosalgeek asynctask diagram

For a Website, this would look very simple because it is a nature of a Web. You do not need to do anything to make it work. However, for a mobile app, not just for only Android app, this process would make you a half day. Why? A request call from a client and a response back can be one private class extending AsyncTask. You might need to make another AsyncTask class to read the data back later on. Let me write some codes to show you. In this tutorial, I use a Login example from Android to PHP to MySQL. It is not limited to PHP and MySQL, you can use your favorite language and database server.

Let’s Create a Generic Class

Why? DRY (Don’t Repeat Yourself). We don’t want to waste our time to recreate 100 of private classes of AsyncTask. We are going to create a reusable generic class.

But before we create that class, let’s start from the basic. I am gonna show the whole process from creating MySQL database, PHP, to Android.

Create a Database in MySQL

First, let’s create a database db_client and a table tbl_client with 4 attributes (id, name, username, password).

Create a Login Page in PHP

connection.php

Below is the connection file:

login.php

Make sure you place the login.php page in the same folder of connection.php. In this case, I place these 2 files in a folder client:

kosalgeek image
Screenshot of login.php Page

Create a Project in Android Studio

Please note that I use Android Studio 1.3.2 (Updated September 2015) which uses AppCompactActivity. Make sure you added the Gradle below in your dependencies.

XML for User Interface

activity_login.xml

The look of the layout should be like this:

kosalgeek image 2

 

REUSABLE GENERIC CLASSES

[Updated on September 27, 2017] Because I have changed the code many times to fix bugs and to support newer Android API, I decided not to include the source code of this library in here. Instead, you can always download the latest version from here, the my kosalgeek github. After you download the GenAsync.jar, you can include it in your library to use it.

Test The Codes

LoginActivity.java

Let’s test our classes with a login page. But make sure you create the LoginActivity inside the same package of the above classes.

Internet Permission in Android Manifest

As you know, in order to make your app able to connect to the Web, not strictly to the Internet, you must add the Internet permission in the AndroidManifest.xml. You can locate your AndroidManifest.xml in app/manifest folder. Below is the Internet permission.

And your whole codes of AndroidManifest.xml should look like this:

That’s it. Now you can reuse the code above in your 1000+ projects without recreating the AsyncTask all over again.

Below is the detailed documentation of the classes.

DOCUMENTATION

Read Data

POST and Get Result

Class PostResponseAsyncTask

Constuctor

PostResponseAsyncTask(AsyncResponse delegate)

At least, you have to call this constructor and pass in an argument of the AsyncResponse object. To pass in the argument, usually you have your activity class implement the AsyncResponse. This class is used to get data back from response. Because it is async-ed, you can get the data through the AsyncResponse’s method called processFinish(string output). In the method, you get the data via the ‘output’ String variable.

PostResponseAsyncTask(AsyncResponse delegate, HashMap<String, String> postData)

This constructor is used for sending POST data into a web server. This constructor can get a second argument as HashMap<String, String>.

 

Additional Constructors & Methods

Please refer to the KosalGeek Github.

DOWNLOAD SOURCE CODE

DOWNLOAD

 

Written By Oum Saokosal

 

Photo Credit:

Samsung Galaxy S4 – dual view by Kārlis Dambrāns / CC BY

Comments

comments