Saving user data into external database with Android and php

Many apps require the user to sign in using a standard email sign in, or social methods. Once a user signed up you probably need to save their data in the cloud in some external database, most likely when you’re creating a social app, showing leaderboards, integrating chat and profiles. Though I have vast experiences integrating Facebook, Twitter and Google+ sign in methods what I’m going to show you now is how to simply save the user’s data in the cloud once they are signed in and how to update their data in case they changed it between two sign-ins. You’ll see that connecting Android with an external database can be done by rookies as well :)

In my example I am using Google+ sign in but that shouldn’t distract you. The point is once we get the user’s data, we save it to our external database and then log the user into our app.

For the uploading procedure we are going to use Volley. If you don’t know what Volley is, please click here to read about it and watch the video. In a nutshell, Volley is much better than Asynctask as it intelligently handles all network related tasks efficiently which makes it faster and last but not least the code is easier to read as well. No more onPreExecute and such! I will soon write an article about how to convert our Asynctask procedures into Volley.

Let’s get started!

First we need to get the Volley library from here. There is unfortunately no gradle dependency for Volley so you need to download the .jar file and add it to your libs folder. Once done, add it as a dependency in your app level build.gradle as compile files(‘libs/volley.jar’) and sync your project. I hope you’re using Android Studio and not Eclipse.

With Volley, network communication is managed by the RequestQueue. The best way to utilize the RequestQueue and all of its tools, especially the cache, is by instantiating it once and keeping it around as a singleton. At this point you can then add or cancel requests, stop or start requests, and access the response cache(s).

Create an application class if you don’t have one and add this code to your app:

Don’t forget to add this class to AndroidManifest as the launcher class! In case you’re confused, this is the same class where you put your GoogleAnalytics code :)

To make everything easier, create a CustomRequest class. We’ll call our CustomRequest() function from here every time when we need it.

Now we have everything to build our code. Create a variable for our .php file that will take care of inserting data into the database:

Once we have all of our data we call

What happens here? Unlike Asynctask, the Volley code runs line by line. First we call the .php script using the CustomRequest class we created earlier. Second, as soon as we have a response we can log the user in. Everything you would put in onPreExecute() goes just before CustomRequest jsonObjReq line and everything you would put in doInBackground() and onPostExecute() goes into onResponse.

Now let’s create the table for our users:

php-user-table

And finally this is our script that inserts the data into our database:

The dbconset.php file contains the access data to our database. We store it in a separate file to not have to define those variables in every script.

Now all you need to do is run the code :)

Need more downloads? Select one of my app promotion services and I will promote your app to 30 quality app review sites and millions of people on social websites.

 

Balint Farago

Entrepreneur, startup enthusiast, gadget fan. I travel a lot and in the meantime I develop and promote mobile apps.

  • David

    Seems that I can’t download/copy the code… So I can’t test/run it. Is that intentional?

    Thanks

www.scriptsell.net