This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Android and the fly likes Android Application- Login Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Android Application- Login" Watch "Android Application- Login" New topic

Android Application- Login

D. Capri Michael

Joined: Jan 16, 2011
Posts: 11
Hi there I'm pretty new to android!
I'm trying to develop an android application which requires login which will link/check with the server/website.
Any suggestion on how can i possibly do it?
Tim Moores

Joined: Sep 21, 2011
Posts: 2415
What do you have so far, and where are you stuck making progress? Communication with the server can be handled by the HttpClient library that is part of the Android API. You should use HTTPS, or encrypt or hash the password before sending it to the server, so that it's not sent in cleartext over the wires.
D. Capri Michael

Joined: Jan 16, 2011
Posts: 11
Oh mainly i just designed the UI. I m stuck at how/which to use for the communication with the server as i did some research over the get/post http method and also json but still not quite sure about it. @@
Prabu Dhanapal

Joined: Aug 12, 2011
Posts: 21

I found a really useful blog for this. It has a sample login android application which sends the user name and the password to a remote server to validate. the server is a simple servlet application. It validates and sends the response back to android app.. It looks really simple and the below page contains complete code as well... Please go through.. Hope this would be helpful...
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

Hi Prabu, the code there leaves a lot to be desired. First, as Tim Moores said, you should not be sending the password out in the free/open like you are. You should be hashing or using HTTPS.

Second, in your OnClickListener you have code which starts a new Thread and uses that Thread to post the login to the server and wait for the response. You then, outside the Thread, immediately get the response to do something with. This is not good. It is true that the GUI Thread should not do long running processes, and that you can't update the user interface in your background Thread - but you never wait for the process to finish before trying to use the response. Either the process is fast and you don't need the Thread, or it is not and you can not rely on it being done when you try to use the response.

A better approach to use the Android AsyncTask. It is better suited to do just this sort of thing.

Finally, I think you have too much code in your OnClickListener, embedding it in the GUI code. You should try to move as much of it as you can out. For example, you have the SimpleHttpClient class - why not make a String loginToServer(String userName, String password) method which does the calls for getting the URL, submitting the post, and waiting for the response, then returns the response. You have that layer so you should use it to abstract out the commands and make them as simple as possible - and you should always strive to keep as much work as possible out of your display code.

For the reference of others, here is the code I am discussing:

Prabu Dhanapal

Joined: Aug 12, 2011
Posts: 21
Thanks for the comment Steve..

I was just thinking of communicating with the server.
The code has been updated with AsyncTask and https support. Please go through the below post. I hope this would be helpful for others.

Jason Mayer
Ranch Hand

Joined: Oct 16, 2007
Posts: 31
it looks like you're new to java, so i'd recommend using an external library such as parse for this. they handle all the security parts and it's free for up to 1 million api requests per month. I'm not new to java and I'm using it in my android app because it made getting started that much easier.
I agree. Here's the link:
subject: Android Application- Login
It's not a secret anymore!