Transcripts
1. Introduction to the course: Hello there. Thank you for stopping by. If you have ever wondered how to create server based android application, this is the right course for you. In this course, you will learn what is a P I exactly in practice how to make your app communicate with the A P I or let's say, with beckoned server. So we will use beach beat as a beckoned server and it will handle the data that we will be sending from our android up. You also learn how to retrieve that data from the database by using PHP script on Android. So this is basically how it will look like we will have some fields where we're supposed to put some information about the user. So we will now add some user so that you can see how it will be inserted in the database for the name of the user. I will put Ross for the age. All right. 33 For the country. I will see Italy, and once I click on insert the data and then refers database, you can see that we have new user inside our database. And if you want to retrieve the data from my SQL database. We can just click on Show the data and PHP script will load these data and send it to our enjoy that. So if you like what you're going to learn in the scores, I see you on the other side. Cheers.
2. What is API and how server apps work?: Now we will learn what is a P I. We will escape boring definitions and other teary parts on We will just see in practice what a P I is exactly. It will be much more clear to you. So first, let's imagine that we have mobile device here and let's say that this here he's server and this here is database for exam. And let's imagine that the user has Facebook application right here open. And when the user tries to register and click on register button, for example, it will send requests to the server with the information's required for registration process. So now, with the part when some programming language in the server candles that request so server can be based on PHP or pie tone or Java or some other language, it doesn't matter, really. So that bunch of code here that is responsible for handling requests from the device and communicating with the database that is called a P I. So it stands for application programming interface on this really means nothing to you, but in what it does is that it is bunch of code written by beckoned developers, and when we send a request from the from the device. It basically hits endpoint that beckoned, developers have said, and it responds somehow. So why is it to go the Why don't we just send from Mobile directly to the database? So it's because this a p I hear or a bunch of code I will call it like that can be accessed by many devices, not only the mobile, so we can build a P I once and we can reuse it, played her even with our laptops or with iPhone device or android device or computer, it doesn't matter. Requests will be same for every device. So when we register from laptop, it will go to the server and and it's cold here will check whether the informations are correct or not. It will make some validations etcetera, and then it will send the data to the database and it will register us. So this is how it works. If we want to see our friends, for example, we will send a request that's called get request. So we will use get request in order to see the data data that will be retreat. Tow us. So that request will go page B, for example. Then PHP will make a query inside a database and then from the database data will be returned to the PHP. And then PHP will give that data back to us. So that's that's basically how it all works. Later, we will see how it works with Android PHP and my scwill as a database to see you in the next video.
3. Installing necessary software: Before we start any coding, we'll first need to download some software's. Firstly, we will download Zump. Zampa will act as a server. It will host our database and peach because that well, right later in the course. So let's see how to download it. So just type in Zampa and press the enter key and click on this download here. Since I use Mac OS, I will download this one here. Remember this one that takes up less space but that uses most recent version off Ph. B. So in my case, it's this one here and let's wait for downloading to finish. So now let's see how to install it. Just double click on it open. So click on next. Next. Next, we will uncheck this next on next. No, we wait. Now click one finish. So now you see this screen here. Now let's click on manage servers and click on Start old and wait for all of these circles to become green. Now open new tub your Web browser and typing following local host slash index dot PHP and President Turkey. If you see something like this, that means that everything he set up correctly so if you wonder. What is this? What's take a look at here? Click one. Welcome and click on Open Application folder and Fight folder named HD Docks. So if we expand this folder, you will see these index dot PHP file on. This is the file that we just opened in our browser. So he opened this file. Here we will see Beach Be script that is running with this page here, so this is the source code of it. So now you can see that every beach be file that we want to run inside our Web browser must be put these HD Docks folder. Only then will be able to run it inside our Web browser. So this is basically that hosting place where we put our files. So let's create new Ph Peaks script and let's try to run it. So for the PHP code, I'm using brackets I d. You can download it searching for it on Google, or you can use your own I d. It does matter, really. So if I open it, we'll click on file new, and we will start writing PHP code. And let's say hello Page B beach bees really easy language to learn. So if you don't know it, don't be afraid. We just needed to demonstrate how to access a p I from our enjoyed up. So this really doesn't matter so much. You can just copy paste if you want it. So no click on file and saved us. Hello dot PHP click on safe. So now we need to find this file by clicking by using right click and show in Finder. So Well, so we'll copy this file in this age the docks folder. So we need to drag it here like this. And now go back inside the Web browser and typing local host slash Hello dot PHP and we can see that it is running as expected. So that's for it. Now, see you in the next tutorial.
4. Creating databse with phpMyAdmin: Now we will see how to create database were really store informations. So first, make sure that everything is running here in Sam. Then go inside your browser and typing local host slash PHP Other mean beach be my other mean slash and enter key. Now click on the new and for the database name we'll see user on click on Create. So we have created the database. Now we need to create table inside it. Table is place. Where will data be inserted? So we'll name the stable, uh, users and we will need four columns and let's click on Go for the first column. We will need I D on It will be primary key here we will choose primary and click on Go and we will select these here and these stands for out of increments. So every time we create new user or let's say we insert new data in these abusers table, this value here will be increment. It automatically Did we? We can't have same idea es for multiple users. And as you can see, Typo it is integer So that's it for the i d for the next attribute or for the next column. We will use name and it will be Who are char the stains for characters. And we will say that can be longer than 150 characters and we will not touch anything else in here for the next column will choose. We will write age so we will need age of the user. We will keep into journey here. We'll need country so we'll just right country. And here we will also need watch our and for delinked. We will also use 150 a no click on safe. So this is the table, he said. Our user database. In the next video, you will see how toe inflated this table with the data by using PHP code.
5. Inserting data into the database by using php: Now is the time to insert the inside our database with BHP called So firstly, let's go and open brackets or whichever code editor that you prefer. So we will start by creating new file click on you and let's save it before we start any typing. Well, save us and let's name it Newser Data and we will firstly saved on the desktop and later on , we need to change this location to the HD knocks that you saw earlier. And also don't forget to add that beach beat at the end of the filing. So in this way, this code editor knows that we're writing peach because so it will have syntax highlighting . So firstly will open tag question Mark Beach. If you don't know PHP, don't worry, it'll because this is on Lee in purpose of demonstration. How are android app? Interact with the beckoned coat so you can only copy and paste what I do. It doesn't matter that much for now. For declaring variables in PHP, be first must put dollar sign and then we can write to me. So firstly, we will well right user name on this year's their name is actually user name off our database and by default it is Route semi colon. Next variable will be server name and it will be local. Host is the default as well. And password. He's empty string. Those data are the data for are exempt and the database name can be found. Hugh. So when you open your web browser, go inside. Local host slash PHP Miami And here is the name of the database the type create. So it's called User. So I offers name is variable and I will write User, Next thing that we're going to do is to establish connection to our server. Somehow, if you didn't know, PHP can be used both in procedural and object oriented way. In this project, I will use object oriented approach. So we will create collection object. Um, you say equal new and my scwill I. So there's the scene techs as constructor parameters, we use server name, then coma. Then we use user name. Then we specified the password. So we referring to these variables and data Baizley like this. So this will be connection variable on at the moment we create this new object connection will be established if those parameters are Okay, So now is the time to write SQL Query that will be executed by PHP code in orderto insert the data inside our my scale database. So this is the syntax. Firstly, we will create some string variable name query and it will be equal to insert into and then we specify the table name. Table name is up users, so we'll type in app users. Now we open records and we specify values that we want toe insert. So if we click on the structure, you can see that we're expecting I d name, age and country. But since we marked idee Toby an outer increment and keys off the stable, we don't need to specify it. It will be automatically put in the database. You will see later how is done. So now we want to put name, age and country. So we will specify here name former age, Korma country. And then we say values and the values will be. Now we will use single quotes so that we can distinguish between those double one. Because if we put double quote here, the stakes after will be Marcus error. So were you single quotes for a string Onda, We will say John, for example, Sickle quote coma age will be 26 coma. And for the country Well put U S. A. So this will be the query that will execute in order to put the data in the database. So next thing to do is to execute this query right here and we will do it like this. Well, Kaiping connection And then this because this is equivalent off duck in Java if you know when we call so method on the object, for example, dog got run in page P. It will be like this. And now we see query and the queries This very variable on the last thing is to clothe this connection will call connection close. So now is the time to execute this code, and this is how it's done. Firstly opens them and make sure that everything is running here. Then we need to open our application folder, which is HD looks, and we need to put our file right here. Firstly, let's find the file. So this is the file so copied. Little bases inside actually looks so now it is ready to be executed on the well executed through our Web browser. Open your tub, right Local host and name off our file, please. User later, Doc HB. So, as you can see, this table is empty as it seems it's empty. But now when we run this page, let's go back to the table on the Refresh it. Now you can see that we successfully entered some daytime here. Now you can see that ideas four. And it's not one that because I have three entries in here before creating this one. I've been testing something, So you should have won instead of four right here. If we If you refresh this page once again and now refresh database, you can see that another John he entered and now we settle for its five. So it's being out or incriminated. So now what is good? It is toe. Put some if statements here in order to check if the connection is okay or not. So if ever happens that you can know what is the problem. So this query method right here also return some bullion value on if this bullion is true. That means that this query has been executed successfully and if it is false. It means that some error occurred. So we will add if we will remove now Semi colon in lieu three equal signs and true. And if it is true, will output record entered successfully? And if it is not, We will, right there. Are that Kurt? So now let's go on the run. But remember that this fire was been saved in Desko. So it means that the one in HD dogs isn't updated yet. So what I will do now. I will copy all of this on that I will just I will start editing this one inside the brackets from now on, so I will just select. Oh, and basically this. So now this file is different from this one here. As you can see, this one is located in a sea ducks, and the one before was on desktop. So now we're free. Reload this page. You can see the telephone message which say's record entered successfully. And if we refresh database ideas now, six. So you can also add if statement to see if this connection was okay or not. So we will just check if connection connect error. We will output the error happened and we will come. Captain A This with the error message and impeach Be recon coordinate with daughter now Just right Connection error. And now let's on purpose change these so route to be like this. So we're on purpose making me drunk now If we were Lord this page you can see there are some errors on its written that our user access is denied because it's not correct User name And if we write it neck and let's know steak, relax, change rallies bit. We save it Every time we do it, we need to save it But I'm using short cook control s core command s and neck If you're lonely now you can see that we have entered successfully like this. Now, in the next video we'll see how to trigger this PHP code to our android up. We don't want toe open page through the Web browser. We simply want to send some requests from they are android up in order to put some data in database
6. Sending request from android device to our own server: Now we're gonna create the up that will be using PHP code that were just written in order to interact with the database. In this first case, it will inserted later. So let's start in your injuries to your project. We'll choose empty activity that was named the up BHP end android. You can name it whatever you like and finish. Okay, so now first we will add some dependencies. Decide are grateful file because we want to send requests. And for death task, we will use library called woolly. So now let's open your Web browser and typing Wooley Android dependency So we can try with this first link. So this is the code. So just copy this and had it right here. Frankly, couldn't sink. Now, Now going South Main activity. And let's start, Cody. Let's create method calls, send request and let's create object called request. You que equals wooly. Don't new request que on this context specify. I mean activity dot this in order to send requests, we need to know you are l over the destination. So if you take a look in here, we're targeting this local host slash user data The PHP address But if we want to send a request from our android app instead, these local host part we must use I p address because Andrew Device doesn't know what is local host. Because the server isn't hosted on our device, but are computer. So in a minute you will see how to find that I p address. But first, let's create string variable. We'll name it. You are. Oh, and for now, let's type in http low co host slash user data dot PHP and semi colon. Now we need toe edit this local host part. So if you are a Mac user, you need to open your terminal and typing I f config and press the enter key on the I P address that we're looking for Is this one here? So it starts with high net usually. And if you're using windows inside your command prompt, you need to type in I p conflict instead off I s skull think that you will find something similar as well. So I will co pay this address here. I will based it. Some of these local host. So this is the part toe our file, actually, that we are sending request. So the next step is to create string the request for the simple task. We will use string requests, but usually we are using Jason requests when we want toe. Send some data, for example, Name, age, country like reading which Beach beat. But firstly, we'll see how to send this easy string requests because it's more simpler. So I will call This request equals new string request. Now we specify the method request that matter that post post metal is used when we want to submit some data. And if we want to retrieve some data from the Web, we're using get requests. Now we will use post because we want to insert the data inside our database. So if you wonder if we can use get request for inserting the data, yes, it's possible. A swell. But post is more secure because when we use post, our credentials are data that's going to be inserted is not shown anywhere in the URL, and when we use get requests, some websites you can you can notice that this your l contains something like equals and and similar, so that basically means that we want to get some data So now we will use post and now a specified the URL. Now we're going to set response a listener. So these responsibly center will return some message from the beach because inside our android app So basically, it's this part here. This is the response. So we will also add ever release in there. So if anything goes wrong with this android up with woolly, we request ever will be displayed here. So we will add a toast message that will print out the response and we will copy Descend basic here. But just instead of response, we'll get error message and the next thing and last one is toe. Add these request to this Q Like this. And now let's just call this send request method from our own creates methods. No, let's go in as Internet permission and let's run the up and it say's that record. These entered successfully. So now you can see that eight is the last one. And if I refresh this, you can see that we have entered the one or more user. So no, I will started the up again just to make sure that everything works fine. And if I refresh. You can see that we have tend to use. So basically what's going on is that request is being sent to this girl and it acts basically the same. If we just have reloaded this inside our VEB Rosa. Now I will show you that instead of this local host you can use this girl also. So if I paste it and press the enter re freshness, we have one more user inserting. So in the next video you will see how toe insert the data from android fields. So we will insert our own data. We'll have some added texts where we'll put information and then we will get those info inside our beach, be script and then we will pass it to the database.
7. Sending JSON from Android to Php backend: we are continuing on from where we left you. So how to send requests in order to trigger some cold in PHP. But as you can see, it's only static data. So the data is already pre build in Ph. B. So we were not able to edit anything. We were just calling this script on ITR Entering name age on the country that is written inside Beach PICO. So it's static. We can't change it from the android that in this video we will see how to insert the data from our android up dynamically. So firstly, we will change. Are you I a little bit Firstly, let's go and add some at it. Texting here. Well, it said hide too 40 dp What? Saying that twenties name on Let's make I d Toby Entity name Now we will copy this one. There is a shortcut controlled the or command the two just duplicate already selected thing . So I will do it once again. Okay, so we can now add this one to the unity age and right here age and this one PDT country and for the heat. Let's put country No, we need toe reposition it a bit like this so we will know. Add some constraints like this. I was going change this a bit. I don't want any margin here here, but I will add some top and bottom merging to this one margin the ports Hey, 12 dp. Um, but on margin now we're also add top margin. Do this one added text and we will convert this text to you to the bottle with ivy bt in insert and for the text. Well right. Insert data. You can see that even though we write inserted data without all letters capitalized inside these bottom they are always capitalize. So could change this All you need to do is to change this attribute next, All caps to force. Now you can see the difference. So now we're back to the android code. Let's declare an initialize, Are you? I components so at it ticks name, age the country And now let's add the button. No, we are going to initialize it. H no. We'll change this one to the country and this one to be me and no, we need toe initialized these buffalo So that's it. So now is the time to send the data from our app to the PHP code. And we will do it by using Jason Request and Jason objects. So if you don't know what Jason is, it's actually collection off key value pairs. So I demonstrated to you like this, so I don't first make some common. Now I will open one curly brace and they will add one closing brace. Let's imagine that Jason object Is this so inside Jason object. We have keys and values. So in our case he will be age and value will be, for example, to 32. The next we add coma and next keys name. So for name, we have value John on the last key value payer is country and for example, Jeremy. And that's it. This is and Jason object. So he re sent some keys and values and later on in PHP, you'll see how to retrieve those values by using those keys that we set up here. So I will leave here, Jason object example. Now we're going to create method that will be sending Jason object to the beach pickle and I will call it Jason. Request that there's parameters we will hit string laying indeed your age. That's drink country. No, we need to initialize requests. Que object like we did previously for the context will specify main activity that this we will use the same you Earl. But we will change this part because we need to create new PHP script for handling our data or, let's say, handling our requests from Android and we'll call it Jason Data. Now we need to create Jason object so we'll create it in this way like any other object. And now we have put values inside. So we really refer to the Jason object and we will say that put. And now we're putting key name and sell you and sell You will be parameter that we will pass through this method. We will see me. So now we will add age keez age on values the one that we will get from users input on the last thing that we want to send these country. So now you can see that theories red underline which sees that we didn't handle a possible exception. So you can just try catch block here like this Jason exception and we will bring stack trace in case anything happens. And now is the time to create Jason. Object to request object. We will do it like this. So now we will specify request Method eight will be post No, we need to specify You are l on the Jason object that we will be sending now. We also need to set some listeners. So firstly, real set response. Listen there. And here we will add air early summer and cynical. So here, real send a toast message your case and thing goes wrong. And here we'll print out the response. And the last thing is that we need toe ad these Jason object request do. Thank you. Now we will add button click listener and we will trigger the smith of every time the user taps these buttons so we will call this method. But we also need to get the info that will be sent so well, do it like this. Doing name equals unity name dot Get a text string our Louis same food country and for the age we need to convert this to the integer so I'll just add interred your dot bar scene All of these inside the brackets instead of streak. Don't put it. And now here we firstly want put name the age and then country like this. So that's it from the android side. In the next video, you will see how toe handle this request in PHP code and how toe inserted the data in the database.
8. Extracting data from JSON and inserting it in database with php: No. Let's open your PHP code editor and let's create in your file we will save this file firstly on the desktop and the name of the file must be the same as one in Android studio that we put in here. So it will be Jason data dot Beach be And I will see what started by opening PHP tags. When the Jason object gets sent to these BHP script, we need to retrieve it somehow. And we will do it in this way. Firstly, we will create variable, cold Jason object, and it will be equal to the file. Get contents now open races and the name is PH. B. It's less lish input Sem ical. So this is the way in which we are going to get Jason object that will be sent to this file . So this is the syntax. Now we need to decode this Jason object. We need to extract the data that we're sending to extract greater. We first must decode these Jason object. We will deco it inside this new variable called date Data on. Now we'll call Jason decode Jason object. So this is how we decode the Jason and now to retrieve variables one by one will do it like this dollar sign. Name is equal. Data this arrow and we will right name. So in this way we're fetching the name that user inputs. You can see this name here must be equal to this key inside our Jason object. No. We will get age by using age, key and country by using country. Don't forget to move this question mark here. If you didn't make mistakes like I did, and as response, we will just sound back these Jason objects that we have received. So we will not send any string value because in under its studio you can see that our response is not strength. That was before, but it is object. And since this is an object off type Jason, as response, we will fetch values back in this way. Response dot gets drink. And we now specify the name like here on the response, though to get think age response, don't get country. So I will expand the windows a bit. Now you can see there was a red underlines. Once again because of Jason exception, you can just click on this in order to quickly fix it up without manually typing in try and catch block. Also, I notice that I have made a mistake here on the instead off itty country. I've been using entity name and here is well, changes toe age. So now everything he's already now let's just for now, seat. If this works or not on that, then we will see how to insert a data inside a database. So, firstly, I will save this file on Onda. We need toe of these and we need to change it with the one in HD docks. If you already put it here since I saved mine on desktop, I will first find it on desktop and then paste it in HD looks. So we got pasted right here. So I need to many we put it replace 50 bucks. I would just to drug this one inside brackets. So now on, we will be editing HC docks file directly. Okay, so let's run the app and see what's going to happen. Also don't forget to comment. Send the request method that we were building in previous lectures. I will put Jack is the name 25 age the country. Let's say Mexico. And if I click inserted data, we gather back the Jason object and we print it out as a post a message in here. So this is the message that we're getting back from PHP. It means that we successfully sent Jason object to our PHP file just to make sure I don't know, change some values. You can see that we're getting those values. So now we will see how to insert these data inside our database. So we will just go and opened this user data files from before. And we can simply copies Connection thinks like this, and we'll base it here. And now instead of those hard coded values, we will use variables that we get from the Android. So now what's at single quote on a dollar sign name to refer to these variable here set off 34 wheel right, single quote, dollar H and here country and say this. Now let's open your Web browser and open your database. After that, let's go and try to insert some data on. I will insert Johnny 31 on England, and if I click insert the data. You can see this weird message I will explain to you shortly. Why's that? But refresh database and you can see that we have successfully entered new user inside our database. So let's right once again with some different values, Joe U S. C. And you can see that it works. Just fun. So now about this weird message that shows up here That's because inside beach be code were not returning Jason object in here. So when we insert the record successfully were returning this string value, as you can see and you know, Andrew it up, we're getting Jason object. So it couldn't extract that string from the Jason object because this is not Jason object at all. It's a usual strength. So now you have seen how to insert the data from our android up by using PHP script into the my skill database. In the next video, you will see how to fetch the data inside our app.
9. Retrieving data from database and displaying it in Android: Now is the time to fetch the data from the database. So let's firstly open records or your code editor on creating your file on. We will name it. Get data dot PHP. I will see you don't desktop. Firstly, now I will copy some of the codes from user data. The PHP I based it here and now we will add it. Something's so we don't need this part here. And now we need to write query for fetching data. So here instead instead, insert into we need to write, select. And now we type which data we want to get. So we want to get idea of the user name, age and country and from from AP users table. Now we will create new variable called statement and it will be equal toe connection. Prepare and we will prepare this query here and then we'll execute this statement. So basically we are. We're saying that the square e will be executed on this connection. Object here. Next we need to declare users are a or the array where users data will be stored. Then we will use a wild statement. And here we will specify that while these statement has results. Basically, we say that by writing fetch we will create temporary all right where it will contain data that we are interested in. So I d will be i d But firstly, we need to bind the results so that BHP know what is dollar I d And we do that by writing statement buying the results. I d name age, country. And here we can continue writing name, age and country. Now we use our A push toe Push this temporary array inside Our users are a So we are inflating these users already with the data that we fetch here on the last thing is the tweet Send these array as response as Jason hurry like this. So no PHP part is done Now we need to go back to the android studio Now we will add one more button because we want toe send the user toe another activity where the data will be Show BTM show day And where will more here? No, we need to create new activity. We will choose anti activity We will know ad tax to you So all the later will be put in this text to you Usually we would use a recycler view. But this is just a demonstration. I will add a mighty Toby. Txt they now we will declared this text you in here, we'll initialize it here we will create method that will fetch the data from our server. Firstly, we need you are l and the U R l will be like this one just with different file names. The file name will be Get data. No, we will form string request method will be Get this time this will be Earl. Well, now create respond, listener, we will add. I really sonorous. Well, since we're sending Jason Array from BHP, we need to get Jason a rain here, so we will do it like this. So basically, we're putting the string response in here, and it will use it to create Jason Array. So a rate off Jason objects. Now we will use four loop to the Jason arrayed or 20 on Jason. Object will be actually our user, and it will be equal to Jason, a raider. Jason, object that Index, You can see those errors and well, everything surrounded. Try catch. So error. Sorry. Gun in the now we will get i d. So we will convert everything two strings to make it easier. So we're basically using key values to age to me, Those are the values that we're sending from Beach be. And now we need to display these data somewhere and we will do it on these tax to you. Okay, so now we will use some trick. So firstly, we will get the current text from our text to you. Then we will go in a new line and then we will use drink for months in order to the sickly the data like this. So those are place holders and we will inflated them with i d with name, country and with age. So now we need to create request que this We will now add the string request to the lease request Que and we will call this method inside our own create method when user transits to this activity on don't forget Boot beach be file inside 80 dogs Fuller in order to make it work. So if I open the app and click on shoulder later, you can see that they days here. So that's it