Notice: A non well formed numeric value encountered in /home/top12rev/public_html/ on line 118

Why Not Generic AsyncTask Class? Before we get started, let me say welcome to KosalGeek. I am glad you came! OK, I got it. You came because you are like me; we are tired of creating tons of classes (mostly private classes) of AsyncTask class all over an over again. Motivation If you don’t know what I am talking, let me explain a bit. Basically, in Android whenever you want to connect to an external source, you use AsyncTask. You can use Handler but it is a different approach which is not covered in this page. Most of the time, we use it to connect to a web service to retrieve data in 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 clients register from your Android app, then there are data send back and forth. See the illustration of how it works: For a Website, this would look very simple because it is the nature of a Web. However, for an app, not just Android app, this would make a day or two. Why? One request from a client is one private class extending AsyncTask. Another response from a web service to a client becomes another private AsyncTask class. Let me write some codes to show you how to do a login from Android to PHP to MySQL. It is not limited to PHP and MySQL, you can use your favorite language and database server. If you know how to do it, you are free to skip to the
Create Generic AsyncTask section. Create a Database in MySQL First, let me create db_client database and tbl_client table with 4 attributes (id, full name, username, password).

Create a Login Page in PHP connection.php Below is the connection file:


login.php Make sure you place json.php in the same folder of connection.php


Create a Login Activity in Android
Check out how to install Android Studio if you don’t have Android Studio yet.
XML for User Interface

Java Code