Best Android Programming Tutorials – Generic Asnyc Task Class

August 24, 2016

511

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

Learn Android App Development for Free with me here at KosalGeek. I earned my Master's Degree in Information Systems from South Korea. I am working as a professor of Computer Science teaching Android App Development since Android 2.0 along with other programming courses: Java, C#, PHP, YouTube and Web. I like working online as well as making money online. You can watch my tutorials at Oum Saokosal Channel for free as well.