Android Login and Registration are very common scenarios. You will find registration and login operation in all the apps where we want user information. In this tutorial, we’ll set up a local web server and MySQL database. We will develop android login and registration application. We will use PHP script to connect to the MySQL database. Show Android Login RegistrationThe first step is to create the backend web server. I am working on Mac OS X and XAMPP can be used to set up a local Apache web server and MySQL database quickly. Setting Up XAMPP ServerXAMPP(or WAMP) is a one-click installer software that creates an environment for developing a PHP, MySQL web application (that we’ll be connecting with our android application). Download and install XAMPP from here. Launch the XAMPP app after installation and you will be greeted with below screen. You can test your server by openinghttps://localhost . The following screen should appear.
Also, you can check phpMyAdmin by opening https://localhost/phpmyadmin . Let’s see what it shows!
OOPS! You might end up with a screen like this. Seems like the MySQL server isn’t properly running. Go To the Manage Servers tab in the XAMPP application and click restart all. The servers should be running properly as seen in the image below.
Now test phpMyAdmin in the localhost and you’ll end up with a screen similar to this.
Now let’s test a sample php script. Create a new test.php file and add the following lines into it.
In the above code:
Note: Knowing PHP is not mandatory for this tutorial. if you’re using a MAC then goto Applications->Xampp->htdocs. Create a new folder here lets say test_android and copy paste the test.php that was created before. Now open the url Setting Up MySQL DatabaseOpen the phpMyAdmin by visiting
If the table is successfully created, you’ll end up with a screen similar to this: Connecting PHP to MySQL DatabaseTo connect a PHP script to MySQL database three input values are required. Following are the inputs and there default values for a XAMPP server
Let’s create a test-connect.php script and add it in the htdocs->test-android folder.
mysql_connect() is a PHP’s inbuilt function to connect to MySQL database with the parameters listed above. Try running Android Login Registration AppNow that we’ve discussed the basic setup of PHP and MySQL, let’s get into the android login application part. We’ll be developing a sign-in/register application. To keep it short and simple we’ll be checking if the username and email are unique during registration. Before we jump onto the app logic let’s work on the PHP scripts and MySQL Database. First, let’s DROP the Table users and create a fresh one in the context of the above application.
Following are
the PHP scripts that you can copy paste in the htdocs->test_android folder.
The script for Database connection is given below.
The following script contains all the core functions of the application.
In the above code, the $json contains the JSONObjects returned. The following PHP script is the one that is called upon first from the application.
In the above code, we check whether the email field is empty or not. If it is, we’ll call the login function in the PHP script, else we’ll go to the registration function. The JSON response returns two params : success(0 or 1) and the message.
Android Login Registration Project StructureIn this project, we’ve used three libs for implementing the HTTP Calls in our application. The JSONParser class is used for doing the POST and GET HTTP Calls to the localhost and returning the response in the form of a JSONObject.Android Login Registration CodeThe
The MainActivity.java is given below.
That’s a pretty big code! Let’s draw the important inferences from the above code.
The
In the above code, we’re calling the respective classes HTTPPost or HTTPGet depending on the the second parameter that’s passed in the makeHttpRequest function.
Above, we are appending the response status code returned from the server in the final JSONObject that’s returned to the MainActivity class. Note: Don’t forget to add the following permission in your AndroidManifest.xml file.
Many Users have
posted their comments at the bottom of this tutorial, stating they’re getting “Unable to retrieve data” Toast. Please note that since Android 6.0 and above you need to add the following attribute in your application tag in the Manifest.xml file: The output of the application in action is given below. Android Registration UserIn the below screengrab we register a new user and it gets added in the Database. We then login using the credentials we entered during registration. Android Login With Php Mysql Final OutputThis brings an end to the Android Login with PHP MySQL Tutorial. You can download the project from the link below. It contains the test_android folder too that holds the PHP files. Copy it into the xampp->htdocs folder! Good Luck. Download Android Login Registration PHP MySQL Project You can also access the full source code from our Github Repository below: Github Project Link |