Android MySQL Database Tutorial 2 - Android Login with PHP MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to the next video on Android MySQL database tutorial for beginners in the last video I have shown you how you can download and install a vamp server and how you can create your first MySQL database using PHP myadmin and how you can write a PHP script to connect to your MySQL database now in this video we will see how we can create our first Android application which can interact with this MySQL database so let's get started just fire up your Android studio ide and we are going to start a new android project and for example we will name it as my SQL project okay so mysql demo okay and i'm going to click Next and leave everything as default blank activity and main activity and finish so now our project is created and in this project in this activity what we are going to do is we are going to design our login activity right so what we are going to do is we are going to take some added text so take a plain added text for example for the person name and take a password added text from here also okay now what we are going to do is we are going to take one button and we will name the text of this button as login okay so just write login button and we can change the name of this button as for example BTN login right and for the added text what we are going to do is we are going to change the ID or of this added text as for example et et username okay et username and for the password field we are going to change its ID as ET password okay and I'm going to leave this username text as black so this is a simple design we want to give a username and password and when we click login it's going to interact with our MySQL database and then we want to you know validate our activity right so now what we are going to do is we are going to go to our main activity dot Java file and in here what we are going to do is we will just remove these two method here because we don't need it right now and in here first of all at the top we will define added text variable so just defined added text and define to added text variable one for username and one for a password so you can just say user name et and the password et right for added text now what we are going to do is we will initiate these added text with our added text so just right username is equal to in the bracket we will typecast added text as we always do so just write add it text and then find view by ID and then r dot ID dot your added text IB right so our added text ID was username right same we will do for the password so I will just copy it paste it here and this time I will take the password and in here we will take e.t password right so just use ET password here now what we are going to do is we will just once again go to a main activity dot XML file and in here go to text and in the button in here we will define on click methods just right on click and let's say it's on login method okay so we want to create a method called on login when we press the button we want to execute this method so just right public void on login and then in the parameters just pass view and then once again you and inside your method what we are going to do is we will take these username and password and get the text from these you know added text right so we will declare a string variable string user name is equal to our username variable which is this one dot get text dot two string okay and same we will do for the password so string password and then this time we will take this and it takes password and then we will get the password from it and save it into our variable okay now what we want to do is we want to validate our username and password and for that we are going to create a new class and we will name it as background worker so let's create a new class here and we will create a new Java class and we will name it as background worker and click OK and we will extend this class with async task okay so just extend this class with a sync task async task and as a template parameter here we are going to give some dummy data types right now and we are going to change them in future so for example void and void and third also void okay and now you can see it gives us this error and when you click this bulb or alt enter it'll ask you to implement the abstract methods so just implement this do in background method and we also want to add some more method for example on post execute and on pre execute okay so first of all on pre execute method we are going to add here and then we will add on post execute and third method we are going to add here is on progress update okay and we also want to add a constructor here because whenever we want to pass this username and password we want to pass it using the background worker constructor right so first of all what we are going to do is in the background worker class we will define a context variable here so define a context and name it as context and now we will define a constructor so just right background worker and as a argument we will pass the context of our class okay we can name it as CD X for example right so CT X okay and then in here what we will do we will just assign this context to our local context right so do it something like this now what we are going to do is we will go to our main activity dot Java and in the on login method what we are going to do is we are going to create an instance of background worker class so just right background worker and then give the variable name is equal to new background vocal and it takes the context right if you remember so context is this okay and then we can just use this background worker variable and call a method called execute on it and this will execute the background worker right so it takes some argument so what we can do here is we can pass multiple argument here right so let's pass three arguments here first is the username second is the password and third is type of operation we want to execute okay so let's define a third variable for example string type and we will define this type as login for now okay and then we can pass it as a first argument for example right it's on you if you want to pass it as a third argument or the first argument it's on you but just remember which argument is it is right second is the username and third is the password right now it's going to give you this error because you can see here this these all three arguments are string and in the background worker we have here only void so we will just change this to string and also this do it in background this data type will be string also right so just change it to string okay and then you can see the error will go okay now in here due in background what we want to do is we want to you know perform our post operation and then we want to you know validate our username and password so now inside you're doing background method what we want to do first is we want to extract the value of first argument which is this type which is login in our case from this param right so what we can do is we can define a variable called string and name it as type is equal to its a param first index right so just right parents at index 0 okay something like this and this will give you the type which is login right so now we can just evaluate this type and if this type equals dot equals login then we can move further right so this is login and then inside our if statement we can write the code to you know post some data right but before that we want to define some URL to you know post to that URL right so if you remember we have created this login dot PHP file right and whenever we you know call this file using localhost then it's you know selecting and you know performing the login function right so we will just call the same login dot PHP file using a URL so you can just tried string and then login underscore URL and then just write HTTP colon slash and then your IP address now you may think that here you just need to write 127.0.0.1 which is localhost or you have to write localhost but what you know Android team or Android the people has decided that if you want to communicate with the local host on your host machine using the Android emulator then you need to use the IP which is this one so just use ten dot zero dot - dot - it's a standard IP address to communicate with local host right and then your file name which is login dot PHP right and then enclose this in two double quotes because it's a string and then a semicolon now inside this if statement we will define a object of URL you can just name it as URL is equal to new URL and then as argument just pass our string URL here right now first of all you need to import this URL from java.net dot URL so alt enter and then you need to enclose this URL in to the try catch block so you just need to add it into the try catch block okay so just hover over here and then click this bulb and then surround with try catch block okay now in here just below this we need to use HTTP URL connection class you may be tempted to use HTTP client or post but you know correctly we use HTTP URL connection so just write HTTP URL connection and give the variable name for example HTTP URL connection is equal to and then you take this URL and then open connection and you need to typecast this URL open connection to HTTP connection so just use this type casting method something like this and then you need to once again surround your code with try catch blocks so you already had it you just need to add one more catch clause here okay which is IO exception right now you can take this HTTP connection and then you can set your request method first of all our request method will be post it's something like a rest request so you just need to write post here and then you need to set some more method for example set output or do output and make it as true and we also want to set do input so just copy this and instead of two output you just need to use dot set do input okay now after that we are going to create an instant of output stream so just write output stream and then give the name to your output stream and we are going to get our output stream from our HTTP connection so just take STD be connection get output stream and then we will create an instance of buffered writer okay so just call buffered writer and give the variable name before writer is equal to new buffered writer and it will take an instance of output stream writer so just write output stream writer and the first argument it will take is our output stream and the second argument is the type of output stream so we are going to give here is UTF minus 8 right and then to use it and then we are going to create our data URL which we want to post ok so just write string and then our post data so just write post underscore data and then you will write URL encoder dot and code so just call encode method here which is first one and then here you will just give the you know key and value pair right so let's say our key is username and the value is the user name when you write so what we can do here is first of all we can extract the value of username and password which we have passed using the you know this background worker username and password right so we have already extracted this type using the 0th index of param and then we can you know extract username and password using first and second index so just take this and then paste it here and this will be your user underscore name for example and the second will be the password password and then this will be our index 1 and this will be index 2 right now you can take the same name here which is the key for your post data and then what we can do here is first of all we need to give the data type which is utf-8 so just write u TF -8 here and then just write concatenation operator and the double quotes equal 2 and make sure that there is no space between them right plus the value of username so just copy the same thing and paste it here and then the value is this one right without the double quotes right and then you just use your concatenation operator once again and then in here we will add the add operator which is for joining the URL right and then in the next line what we can do is we can copy the same thing from here up to here because we don't want to and right now because it's the end of the URL and just close it and this time this will be our password field so just write password and password so this is our post data URL make sure there is no space here and you have and here now we just want to write this post data to the buffer writer so just use buffer writer dot right and this post data will be the argument for this and then just use a buffered writer and then flush everything so just call flush and then once again call this buffer writer and we will close it okay and we also want to close our output stream so just use auto stream dot to use okay now when you post some data on to the server you can expect some result also all response also for the post request and to read this response from the post request what we can do here is we can once again define an input stream and then buffer reader to you know get the response so this time we will define a input stream here so just define input stream and then the variable input stream and then we can get the input stream from the same HTTP connection so just write get input stream and once again we will define a buffer reader this time so just write buffer reader take the variable name is equal to new buffered reader and as the argument new input stream reader right so just write input stream reader and as an argument it takes this input stream and the type of data at this time this data which we are expecting is of this type which is is oh- 8859 minus 1 and now once we have our response we want to read it line by line and make our response from it right so we will just define two more variable one is result for example and other is string line to read the line line by line and just write while so just add a while loop and we will you know read the line line by line so just use line is equal to buffered reader dot read line is not equal to null so is not equal to null then we can add this line to the response right or the result so just write result plus equals this line so something like this ok and something like this and this all data should be in we'll bracket so we are evaluating this and we must also initialize these value with normal for example or empty string okay so just initialize line and result by empty string okay and once our work is done what we can do is we can close our buffer reader so just close after your while loop the buffer reader close and then we will close our input stream from here so just write INRA stream not close and then we will disconnect from our HTTP connection right so just write HTTP connection dot disconnect okay and in here now we want to return the result which we have got right so just write return result now it's going to give you this error because our return type if you can see here is void here so change this return type of this method do in background to string and this will give you once again the error because we need to change this return type from here and here also string and the async task types and also we need to change it here in here right in post execute right so just change it here also okay now to show the login result we need a alert box or something like that so what we can do is we can go at the top and in here we can define an alert dialog and give a variable name for example alert dialog and then press alt enter to import alert dialog and then we can initiate this Allah dialogue in here pre execute method okay so on a pre execute method you can just write instead of this line you can just write all dialogue is equal to new alert dialogue dot builder so just write builder and then take the cause context which we have defined at the top so this constructor is taking the context right so just take this context from here and pass it here and then dot create okay so just call a method called dot create and remove these brackets from here okay and now you can also set some title to your alert box so for example you can say login status okay so this will be the title of your alert box now to show the result in the alert dialog what you can do is you can go to on post execute method and in here you can just remove this line and this string can be result and then you can take your alert dialog and set the message here so just write set message and just pass this result here and then you can show the message which is coming from the server right so just write a lot dialogue dot show okay so this will show the response of the server so what code is almost done what we need to do next is we need to go to our manifest folder here and go to Android manifest dot XML file and we need to give the internet permissions here above this application element so just give this line of code to allow the internet permission so just add user minus permission space Android name is equal to Android dot permission dot Internet and this will add the internet permission to your Android app one last thing which is remaining is we need to add it our PHP files right so go to the connection dot PHP which we have made and if you already forget there is the location of this connection dot PHP it's in your vamp folder in your C directory and in www you can find login dot PHP and corn dot PHP right which we have made in the last video ok so first of all we need to remove this echo information from here so just remove these lines of code from if condition till your end of your if condition right in the corner dot PHP ok and save it and in the login dot PHP we have given this static information to login so to have the post you know request received here you need to replace this by dollar then underscore post and then in the square brackets so just use square brackets and then your key name which you have defined in your Android code right so go to your background worker and once again check what is this key value which is username here right the first key value okay and place it here and in the same way we will just place the password key value from here which is this one in the double quotes right so this is value in the double quotes right so go here and change it here also and save everything and then run your code okay so my app is running now and I'm going to define the username and password which I have defined if the database which is one two three and one two three so I will give this one two three and one two here and when I click login it doesn't reply me so this can be the problem sometimes if you have this ten point zero point to point to IP address defined here in some cases it will work if you are connected with localhost or if you want to connect with localhost otherwise in most of the cases it will work otherwise what you can do is you can open your command prompt so just open your command prompt here and then just write IP config here so just write ipconfig and press enter and it's going to give you your IP address of your local network and as I can see in my case the IP address here is 192.168.1.0 right so you just need to search for wireless LAN adapter or wide LAN adapter and search for this IP address which is the first address which is 192.168.1.0 light and then replace this by 192.168.1.0 it can be different so just see this value and paste it here once again we are going to run our app and our app is once again running so once again I will give one two three username and password one soon one two three and then click login and now I can see this login success message which is coming due to this line of code ok I can just change this line login success and then some exclamation mark and then some some message login success and for example well come use something like this and then save it once again click the login message and now you can see login success and welcome user here okay so I know that this is coming due to this and when I give the wrong username and password is going to give me login not success which is this message okay so in this way you can create a login page in your Android application using MySQL database I will also you know give this code in the description so you can you know copy the same code and paste it into your application and test it okay so just click the link in the description and you will get the code from there so please read them and subscribe and bye for now
Info
Channel: ProgrammingKnowledge
Views: 396,697
Rating: undefined out of 5
Keywords: Android, programming, tutorial, beginners, develop, mobile phone, Environment Setup, Application Components, Activity Lifecycle, Service Lifecycle, Application Architecture, Publishing Application, Debugging Applications, Handling Events, Layouts, User Interface Controls, Styles and Themes, MySQL, Database, PHP
Id: eldh8l8yPew
Channel Id: undefined
Length: 33min 55sec (2035 seconds)
Published: Thu Jan 07 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.