Xamarin Android Tutorial 19 Inserting data into SQL with Web Request

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody Welcome to the 19th samon Android tutorial and this will be continuation of the 18th tutorial in which we set up our contact okay so we have our fragment that comes in and we simply make a contact and then we now have the ability to open up our gallery and we scale down this image to a thumbnail so that we are using memory efficiently okay so that's where we are now now in this tutorial what we're going to be looking at is actually sending this contact to the server okay on creation so as soon as we create it we're going to send out to our server to save in our database using a web request okay so let's get started on that now the project file the start if you look on the link below you'll have a file that you can download and it'll look just like this so it'll act as a template as a starting point for what we're going to do in this tutorial okay guys so the first thing that we're going to want to do is we're going to want to go to our dialogue and this is what comes up when we SE when we actually hit the add button so this is the class that takes care of that so this is where you actually input the number and the name and create contact okay and here is the button where you actually click and create the contact okay so this is going to be the majority of the code that we're going to put in here okay so inside of here we're going to want to actually do our web request all right so the first thing that we're going to want to do is use a web client okay so let's go ahead and make our web client all right so use web client and this is basically a subass of a of our request and and so the web client Inn net has a few more goodies and just kind of lets you do a little more things than the web request does because but everything that web request does and the web client can do a little more so using the web client I like to use it's it's very simple and straightforward so let's go ahead and instantiate a new instance of it all right we'll need to import that system.net all right the next thing we're going to want to do is we're going to want to create a URI okay so where is this file where is this client going to okay so we're actually going to use PHP to to talk to our server okay so we'll get into that shortly and this is just the IP address on my local area network for the server that I'm talking to Okay so this could be the Local Host this could be your own this could be you know a server that that you communicate with and you just have all you need to do is put this file on that server okay so once again this could be you know your own computer for me it's not it's actually another computer on my network so I'm going to go ahead and connect to that that's the one that has my SQL server and my PHP on that server is that's where I do a lot of my uh networking at okay so but once again this could be something else all right so this is going to be your own I IP address all righty so let's go over to create contact before we go any further in C let's go ahead and go to this PHP file all right and here it is okay so let me just I have it already written out but I'm going to go over it just really quick to to go ahead and make sure that we're all on the same page of what's going on some of you guys might be familiar with PHP some of you guys may not be so I'm just going to do over really quick and and just make sure we're on we're all on the same page there so first we want to make sure that we're going to send some post variables okay we're going to send that the name that the that we make the name of the contact and we're going to send the number okay so these are going to be some post variables that we're going to send all right and we're going to basically take them into and put them into our own PHP variables using this so the first thing we want to do is make sure that these are actually set okay so if you go to like straight to the URL basically if you go just open this up if you go to 192.168 and then you go to create contact nothing's going to happen because it's there but essentially this never these these post variables never get set so nothing ever happens okay so that's all we're doing here so that we can only get to them if you to actually specify the variables all right so once again we get them into our PHP variables and then we use connection info okay and this is where we include it from all right and connection info is important that's what actually makes your connection to your server okay so I have that into a separate file so you're going to want to download both these files and I'll and I'll make sure that I have a link that you can get to them and you want to put your server name which right now the PHP is the server and and the and PHP are going to be on the same server so I can use Local Host all right and then you're going want to put your database name which mine's called just test and then your make sure you put your username and your password okay so fill in this stuff before anything and then this way you can authenticate yourself with your server and get into it and make the connection okay and that's all that's doing next the thing we going to want to do is we're going to want to check to make sure that we made the connection if not we're going to Echo out to the client that hey I can't connect otherwise we can't connect and we're actually going to query so we're just going to do a simple query where we insert into our contact table the name and the number okay and then the values are going to be there going to be two values name and number and we use parameters we use an array basically it takes the name of the number and that that wires up and hooks it to these guys right here okay and this is using SQL Server I'm using SQL servers uh basically API so this is Microsoft plugin for PHP to communicate with Microsoft SQL Server okay this isn't PDO this is their own so just make sure that you guys can do you know if you guys are using PDO or you're using MySQL there's going to be a little different syntax but essentially the the the query and the way you hook up parameters going to be a little different but it's going to be the same way way you do it there okay so but just wanted to make sure that you guys are on the same page that I'm using SQL server for the for the plugin for to communicate with my uh SQL Server okay so we want to make a statement we execute the query here's where we actually execute it and then we check to make sure that everything's okay if it's if it's not it'll query out that it failed otherwise it'll come into here and this is important it'll do another query and what it does is actually gets the ID that was just created okay so we created a contact here and we basically take it and we say hey what was the ID of that new ID that or the ID of the new contact okay and we so we make another statement we just reinitialize it and then we get the new ID okay so we specify that here and I just make it as new ID and we get the ID of the new contact okay and we Echo that back to the client okay so in this case the the mobile phone all right and I'll show you why we do that in a second but just remember that we do get back the ID from SQL Server okay so that's what this script right here does for us okay so let's go ahead and go into our SQL Server database okay and this is my test database and I'll open up the contact table all right and you can see that there's one contact in it all right just some test stuff and uh that's basically it okay we don't have an image yet we're not going to be focusing on images we're going to do that in the next tutorial because it's going to probably take a whole tutorial to kind of explain it's a little different for images so we will be doing images in the next tutorial but right now we're just going to do something simple to start off with just some string data okay so and it so here is basically just just four columns name image number and the ID number that's passed back when we create a new contact okay so that's all this is right here pretty simple and straightforward all right so just make sure that you get that PHP file these two files and put this inside of wherever your your server is running I'm I'm running a patchy on my server so I just put this in the root document and just make sure that these are in the same directory okay and if not if they're not in the same directory just make sure you specify that here you know directory connection info okay so M mine are both in the root file okay just to make it simple all right so now we can come back to our C code all right so the next thing we're going to want to do here is we're going to want to create a name value collection all right and this is really really nice this makes it really simple okay so we'll need to import that and I'll show you why here in a second all right so then now all we need to do to add our parameters our post variables is do parameters. add and we're going to use this second method that takes a name and a value so it's basically a key value pair okay and remember right here in our post variables this is going to be the key and we're going to give it a value and then this is going to be another key we're going to give it another value okay so we got to make sure that these these strings match exactly the same as we as we do in our C code all right so we want to make sure that that's that's matching exactly okay so we'll do name make sure that it matches and then our value which is going to be from what we get over here so txt is just an edit text that we actually entrance that's going to be the text the user enters in okay at that point in time all right so we want to just do that again for the number all right now that we have that we have our two parameters so that's going to be all we need there the next thing we're going to want to do is we're going to want to subscribe to an event okay and this event is called upload value vales completed okay and we'll do that right over here and this guy gets called essentially when the when basically the task is done okay so it's communicate with the server and it's got a it's got something back okay so when this whole when the whole web request is finished this will fire okay and then we can execute some code afterwards all right because we will be doing this on a separate thread okay and that's what we'll do here so what we're going to do is we're going to want to do client. upload values async all right and it takes a UR which we've specified and it takes a name value collection which are just our parameters okay so there you have it this is actually going to execute our web request on another thread okay and then when it's finished it's going to come down here and do any kind of cleanup code all right so in that case what we're going to do is we're actually going to move this code over to here all right and the reason why we're doing that is because we want this to do its own thing and do it web request and then over here once it's actually finished is when we actually want to broadcast to our to our subscriber which is over here in the main activity remember down here so this gets called because we subscribe to it here all right and then this method gets called and this B Bally just just meant to update the UI okay so this isn't doing no requesting it's just going to update the UI and actually add the contact in our list view okay so let's go back over here so that's going to do it here because if not it would do it right away and then it wouldn't really make sense because hey what if there's an air what if there was you know what if we're updating the UI and there may have been an error or something like that so we want to First make sure that everything is completed first okay which brings us to our next thing uh the next thing we're going to want to do is we're going to want to actually get the ID that's returned okay remember back over here we return the ID okay the new contact ID all right so this is where we actually retrieve it all right so we want to do encoding utf8 we want to do get string okay and the g string method takes a bite array all right and that exactly is exactly the data type of a result is a bite array okay so this takes a bite array and converts it into a string all right and that's our ID number all right and our ID is going to be an integer so let's do let's make an integer all right and then we'll do int. tripar which is a really nice method it takes it and it will return false if it can't if it can't parse it or cast it to an integer all right so we're not going to check for false but if you wanted to then you can do that and it's really nice so now our new ID will take the value of the string ID okay and that that's all that's doing there all right so the next thing we want to do is we're going to want to actually add a ID number to our contact okay so remember over here in our contact class we just have a name number and image so this represents a single contact all right so then what we want to do is we actually want to add the ID so that way we can keep track when we actually if we're going to update which is going to be the next tutorial we're going actually going to update a contact we're going to need to update by ID so when you when you you know when you update contact number blah blah blah where contact ID equals one so we're going to want to make sure that we have the ID registered so we know which one to to actually uh update when it comes time to do that okay so we'll call it ID oops it's going to be of integer type so ID all right and that's all we need to do there now we need to come back into our event args okay and remember our event args are just what's passed into our our event okay that's that's broadcast okay so right now we have the the name and the text okay so in our create contact event args it takes a string name and a number okay so we want to actually take the ID now okay so that's all we're going to do right here we're going to add again we want you to take an ID as well all right and then we're going to go ahead and specify that here ID equals ID all right so that's still that's all updated good to go now we should get an error here because we've added another parameter to this Constructor which is fine what we're going to do is we're just going to pass it new ID all right so that makes it happy all right and the next thing we're going to want to do is this is going to remember update up our UI and this our UI is going to to go ahead and update the UI and actually create the contact okay in memory all right and that's what we want to do here so we do new contact now now in our event args we now have an ID number see that so we're going to want to add that to our contact remember the and we just added the ID so we're actually going to initialize it if we don't do this then the ID is just going to be like null or it's not going to be null it's just going to be um just this maybe like zero or something like that so it it's not going to work and we're not gonna be able to keep track of IDs correctly okay so let's go back here and we'll simply just ID equals e.id okay and that will take care of that all right so that way every list every item in our list view will actually have an ID number so if we update it we can just call upon the ID and and and take take that to our web request and feed that in okay as a parameter which we're going to do in the next tutorial okay so let's go back into our dialogue all right and let's go ahead and make sure that's good all right so that should all be good okay and we're going to run this and we're actually probably going to get an air but I want you guys to see the air and we'll fix it and that way if you guys run into that some sort of air you may know how to fix it and then be more prepared for that okay so let's go ahead and see what happens when we run this and we'll we'll see what happens okay bring this up okay so let's make a contact and we'll call it test contact just give it some bogus number okay so argument cannot be null parameter name b all right so more than likely what's going on is the it's it's kind of weird when you don't run on the UI thread what's going to happen is it's going to start causing some weird things like this method gets called twice and I'm not sure why but I I was banging my head on it like a while back and I'm like why is this getting C what I don't get it you know and it was because I wasn't running on the UI thread and the upload values complete or any kind of any kind of method that's called after a thread's finished a separate thread not the main thread I always try to make sure that I'm doing a runon UI thread because I start getting some really weird errors okay so just wanted to show you guys this if you guys do get something like this ever in the future more than likely just try to run it on your on on UI thread and that that hopefully fixes it okay so let's let's go ahead and and fix this problem right now all right all right so the way we do that is we can't just do run on UI thread because we're actually we have a fragment okay not a not a activity okay but what we can do is we can actually get a reference to the activity and then call upon its runon UI thread method okay and we're just going to going to do it here in line okay so this is going to be our action that's a little that's some Syntax for it and then anything in here and these curly bra bracket braces will actually run on the UI thread okay so we're just going to put everything in here okay and everything's the same it's just that's going be running on the UI thread and that's just a safe way to do things and at this point we we want it to okay so let's go ahead and run it again and and let's see what's happening now all right make a contact call it test again once again just give it whatever whatever kind of number okay so no errors or anything let's let's let's go back into our database and we'll make sure that it yeah so it right here inserted and our test contact inserted too but we just got a we got an error on the client not it did go through in the PHP so that's good but here is the full uh correct way to do it okay so this made sure that it's got in and now it's it's going into here and everything's good okay the last thing we want to do um if you want to do it is we want to add some kind of feedback to the user and that's just a progress bar okay so remember our layout file for I for an actual dialogue is right here in our dialogue sign up here it is and then do remember that we do have a progress bar right here okay and our ID for it if you don't remember is just progress bar yeah okay so the way we do want to do that is we want to probably know by now we just want to grab an instance of the progress bar call it progress Bar M progress bar view find view by D okay so now we have a reference of it and now we now it's it's it's actually Invisible by default okay so when a user clicks on this create contact we're going to start the web request okay so at this point it's safe to basically go ahead and changes visibility to visible okay and we can do that here all right and then now over here we don't really have to set it to Invisible back to Invisible because it's going to dismiss it and then it's going to go away anyway all right but if you did did want to it's safe to do it just make sure that you're doing any kind of UI updating you want to make sure that you do on the UI thread okay and so we'll change this visibility back to Invisible all right and there you have it so let's go ahead and try that one more time to make sure that our progress bar is showing up and and going away as it should this is is a good way to just provide some feedback for the user to make sure that like you know if it does take a long time they might think hey it's frozen you know my application's uh breaking it's you know it might just be taking a long time because they might not be able to reach a server efficiently okay so call test progress all right cool all right so the progress was there and then it went away it was actually really fast but uh it was there and then now it went away okay so all that's good there and we are good to go okay so notice one more thing is obviously it's pretty it's pretty obvious that that as we exit out this application our test progress or any other application or any other contact that we made disappears and that's because right now it's just a memory okay so in the next or the next actually two tutorials after this one so not next one one before we're actually going to be grabbing the data so we're not going to be inserting we're not going to be updating data we're actually going to be selecting data from our database and feed it it into a list view dynamically okay and that way anything we we create goes into the server and then comes back and then we can feed back into it and then it'll have all of our contacts that we created from from the server and then we'll we'll have like you know any images that we did create it so it'll keep it'll save all that on the server okay CU you know more than likely that's the way it's going to happen the world world is you're going to want to save stuff on the server and anything here is just going to be stuff in memory for the point for the time being and then when it dies and you open up the app again it's just going to reach out to the server again load up the contacts and feed them all here okay so we're going to do that in the next couple tutorials and the next tutorial following this one we're actually going to be able to take this set an image and actually that's going to do an update to the server to update this progress bar with the ID number to save the image so that way when when we select from it it'll save the image as well okay so there you have it guys there is the web request and if you guys have any uh questions about like the PHP or anything like that or the library I use or just any kind of questions feel free to put some comments below and and I'll be I'll I'll be more than happy to answer and do my best to to get you guys up and running all right thanks for watching guys
Info
Channel: Joe Rock
Views: 105,191
Rating: undefined out of 5
Keywords: Android App Development, SQL (Programming Language), Tutorial (Media Genre), Android (Operating System), Software (Industry), Xamarin (Business Operation)
Id: jF3D__ibrx8
Channel Id: undefined
Length: 24min 42sec (1482 seconds)
Published: Thu Nov 27 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.