I am starting to work more with AsyncTasks and realizing that they are used for almost everything. However, I just started doing Android development, so I am by no means a master. Last night I realized that I was doing my login(and all http traffic) wrong by doing in on the UI thread. After researching for a while I finally figured it out. So Im putting together what I found so you can find this and quickly do it now.

Side note: Im using HTTP headers to speed up my response times, so instead of sending extra text like “success”, it taps into the already sent header code. Read more about header codes and their definitions.

You might ask: “Why use an AsyncTask?” The big difference is for the user. When you call a HttpClient.execute() call, it freezes the call stack (think about how you can’t interact with a web page until it loads) and with it the UI. With an AsyncTask you execute long processes in a thread, which allows you update your UI in the middle of the call.

Step 1: PHP

This is where you are going to have to change things. This php file is set up to access a MySQL table and check if the user email and password match those inside the DB.

Step 2: LoginActivity.java

Every Android developer knows Activities but one thing important here is are the last two methods which are called by the AsyncTask to change the UI.

Step 3: LoginTask.java

Here we have a class that extends the AsyncTask class which will run behind the scenes and talk to the server while you keep your user engaged with a cool progress bar/wheel. The important parameter “activity” of type LoginActivity allows you to call its methods login and showLoginError.



blog comments powered by Disqus

Published

20 November 2011

Tags