Android Volley Tutorial (Android PHP MySQL) - How to Read/Send to Server (Part 2)

Android Volley Tutorial (Android PHP MySQL) - How to Read/Send to Server (Part 2) - Hello friends Android tutorial and tips trik, The article you are reading this time with the title Android Volley Tutorial (Android PHP MySQL) - How to Read/Send to Server (Part 2), we have prepared this article for you to read and download the information therein. hopefully fill posts article -, article (Part, article 2), article android, article How, article MySQL), article PHP, article Read/Send, article Server, article TO, article Tutorial, article Volley, we can write this you understand. Well, happy reading.

Title : Android Volley Tutorial (Android PHP MySQL) - How to Read/Send to Server (Part 2)
link : Android Volley Tutorial (Android PHP MySQL) - How to Read/Send to Server (Part 2)

Baca juga


Android Volley Tutorial (Android PHP MySQL) - How to Read/Send to Server (Part 2)

Hello everyone, my name is Oum Saokosal.

This is part two of how to connect androidwith PHP and MySQL using volley.

This is where you can find me on YouTube,Facebook, Twitter and Github.

In the first video, I talked about how toset up a volley library, how to make a singleton class and how to do simple request.

In this video, I’ll talk about how to sendand read data from/into PHP and MySQL.

I will cover post and get.

I will talk about how to put parameters intothe request and also I will talk about how to do the HTTP header request and lastly Iwill talk about how to handle the errors.

Okay! This is the previous video, we could sendtwo requests, this one is the single request1 and we call to google.

Com.

Kh.

Kh is for Cambodia Khmer and second one isrequest to the local one, so make sure you are running the XAMP and if you are runningWAMP, it’s ok.

And this one, the first one to Google andthe second one to local.

Here you see the second one to local, stillit runs fast because the local should be faster than the global one then it read and retrievethe data and then sends back to android.

This is how it works, so it is really a multiplethread.

Don’t forget to put the internet here andalso for the volley you have to put this into the gradle.

Okay! So now we will continue our lesson, so nowI wanna talk about the post and get.

So here like I said in the first video ifyou don’t mention it, the first parameter by default it’s a gap.

So now let’s try to create a PHP page andtest it, let’s go to the htdocs and then I have this one, and let’s try to createa file, and I just got call it text.

Php.

So you say a request method, we call “Get”so you wanna test whether it is get or post, okay, you just echo something , so just callfor an example, say “It is get” and here echo “It is post”.

So for android, you have to specify the pathhere because I create the file its test and then I create a string, number three now andthe url3 and put the request here, you don’t have to delete this one because this is aseparate code and just comment this one.

It’s good to go now.

Okay.

As we can see now, the return string is “Itis get”, it means that if you don’t specify anything here, before one it is GET, so nowI want to say, I want to call it to the POST, what you have to do is, you put the request.

Method.

Post.

Okay, the post one and if you still want touse get, just say GET, because GET is the default, so now you want to test again, againnow you can see which is post, so now you know how to send a data by GET or POST, ifyou are not sure what is POST you can go to Google and find post vs.

get, and this oneexplains it well from the w3shools here, so GET request data from a specified resourcesand POST submits data, so the big difference is here, you want to submit data and GET here,request can be cached and POST never cached and GET can remain in history, can be bookmarked,should never be used when dealing with sensitive data.

Okay AND this one is okay to send the databut still you want to use the https, there is no guarantee that host will send the dataencrypted and so you have to use the https:ssl socket.

Okay.

But for android because you don’t have anybrowser here, you just call it In-android app, in website you have this and it’s aboutthe bookmarking and some other like history thing, but here in android these two are notso different, okay, but if you think that maybe you want to put some data and then maybeyou want to call it from a query string like this, maybe you want to say lang is English,for example and maybe time is UTC+7 for Cambodia then you want to call like this then it’snot POST, it’s a GET.

But if you want to send data by parameterswithout this, you have to think it is a POST, so for me I prefer to use POST even thoughit’s the same thing but POST for me is always the better.

Now we move on to parameter, so let’s saynow you want to use POST here and you say if now let’s say POST and you want to senda parameter and which is the key, maybe some username and then maybe if username equalssomething, maybe just kosal, but I always test if the key is existing or not, so inPHP you have isset and you say POST like this and username, so make that the username, keyis exists then you can compare like this and then maybe you want to test the password aswell, like this and the password is “123” string and then here you say correct, okay,and else incorrect.

Okay! Now you want to send a data here, to senda data you have to call by getParams but you have to override this string request but inJava, now you have a better way to do that overriding what you have to do is open thecurly brace and close the curly brace after the constructor like this, and then here,you say getParams and you can see now it’s a getParams here and just double click it,now you have a nice getParams method, and here is the hashmap, you want to declare amap like that and I say params and new Hashmap<>(); and params.

Put.

Now it’s a key, the key here is the sameto this one, so it’s a username and the second one is a value and what is the valuenow, its value is kosal right? and you want to put second key and password and now youwant to return params, okay, that’s all, now you can send the data to the PHP web serverand then the data will read it and PHP will test it, it if it is correct and it will echocorrect if it incorrect and then echo incorrect.

Now you can see it is correct, now you cando something with the result now because it is correct already.

Let’s say you have this and I want to makesomething more interesting, I want to have like a textbox and a passport textbox anda box here, we name it, so etusername, okay, etpassword, this one is login, this one islogin and if you give the correct username and password and you click login, it shouldgo to another page, so you should have another activity here as well, Newgo to Activity hereand you can use the navigation, let’s stick just stay with the basic one, and choose thehierarchy, the main one, Finish and into the sub, I should have some text, let’s saymainwelcome, so here in main, so I have to get the data from the text so I have to clearthe edit text, etpassword and around here and for the username and password, it is likebefore and then findViewById.

Now you want to call the event, Click buttonon the btnlogin, so here, btnlogin setOnClickListener, new and space.

So you want to cut the whole code here, andpass it in here, okay, so now the problem is the ‘this’, so we just got the ‘getapplication context here’.

Should be nice now, and you want to changekosal, number 123 here, the etusername.

GetText.

ToString.

Etpassword.

GetText.

ToString.

And here you see if the response is correct,here the response is correct now you know that echo is correct, now you want to testthe response here, so if response equal correct but I don’t want to use equal here becausesometime you miss to leave a space after that or maybe there is some space, like now wehave two three space like that, so usually I just say contains so because the word “correct”should have some you know what do I call words in the page, okay maybe correct_login, sayand this one is more precise, okay, correct_login here, else here, then you can go to the secondactivity here, intent in new intent (main activity.

This, subactivity.

Class) and yougo to StartActivity and it’s now an error or call it “incorrect username and password”.

Okay.

Okay.

Now let’s try kosal again 123.

Ok now you know you can go to the passwordand it says you log in with the wrong one.

Okay, now you can go to the subpage becausethe password and username is correct, let’s say if you login with the wrong one, okaythere is some problem now, I got some problem with the PHP, so I should change the codeto this one, I should test the isset first if the username and password is exists, likethis step, then I can test like username equals kosal and password equals 123.

It should be like this and in the real worldactually you have to test again the database but this one is just for example, I don’twanna go deep into the php, we are talking about android and we should have a knowledgeabout this and if you don’t know this maybe you should watch another video of mine likehow to do the login like this.

Okay! So now we test it again, if I test it likethis and incorrect username password 123, so now it goes to the subpage.

Okay, so now we move on to the header, sowhat is header? Okay, if you don’t know just headers, youjust type http headers example and there is a good blog here from tutsplus.

Okay, So he explained like in a picture, okay,so when you do the http request, it send the http header okay along the way and when youget the response and then you can get the status line and headers and content, so usuallyyou see the content you don’t realise that you have the header.

Header is something like this, you will seethe GET and you will see the URL, the HOST name and user Agent, Accept language and code,other thing, even the cookie, cache and your web server want to know that if your clientrequest is from where? Maybe from iPhone or from web browser butwhich web browser? you can see now its Mozilla.

May be he might be using Firefox.

This is the content and you can see the headerand I recommend you read this but I will not go down much I will explain a little bit aboutthis, so what most people are interested with http headers is about the User Agent, it’sa way that tests against the clients call, by the way, which client call, which browserand which platform? In this part let’s say you test it in thePHP say SERVER [“HTTP_USER_AGENT”] and the strstr, it means like try to find thisIE6, inside user agent because user agent has a long list like this and then you wantto find a string of PHP, the PHP function to find the string and then you call a serverand user agent, remember if you don’t know this because you usually see the code likethis, http, you see this the user dash agent and then you want to call it in PHP, you callit HTTP with the capital one underscore USER_AGENT, its always like that.

If you want to test again the ACCEPT LANGUAGE,you say HTTP_ACCEPT_LANGUAGE in the full capital and now if you want to test the French oneor the English one, then you can rewrite to this okay.

Some are examples about this and is aboutthe content type and the charset, most people get a problem with this because they don’tput the charset as UTF_8, so they cannot get the data from the API.

Okay.

So, now let’s test it.

Now we just use user agent only.

So where is user agent? Here, In android you can just do it like thatby overriding class here, you say get header, see you get the header already, and it isthe same thing because it is a map, so map<String, String> and now I say header new HashMap likethis and headers just put something and now you want to put the user agent, okay, Userdash Agent, and what is your agent now? You can just tell PHP that my agent is android,so I would say MyAndroid, that’s just an example here and then I return the header.

Okay.

In PHP, it is just for testing the GET andPOST here.

Now, so maybe I’m not interested much tothis, so let’s comment this, so just say server and http underscore useragent likethis and now my user agent is “MyAndroid”, okay and then I say “Return Code for Android” or else “Return Code for General Platform”,okay, so now we don’t want to test it against the correct login or whatever, so we needto comment this, now just Log.

D (TAG, response) is enough because we just want to see whetherit is an android or a general one, so now we put the code in the login, just click login,see now return code for android.

So it’s a good way because you don’t wantto have a separate page, let’s say for android and for user web.

Let’s say, here you go to localhost/customer/test.

PHP and now because you are using the generalone, the code should be for the general purpose but then if it’s for android, you can seenow it’s an android code, maybe the charset is different, maybe the image is differentand the size for the image for android should be smaller, okay, so this is the way thatyou want to test again the header and then you can put some other things you want.

Okay, let’s see the example here, now youwant to know which language, okay, so this one, so you say accept language like this,okay, here you say English as its your data here, so say English like this and go backto here and quite similar, HTTP_ACCEPT_LANGUAGE, it should be the same like this and “Englishpage” else you give my page “Khmer page”.

So you can test it in here as well, okay soyou can see now, its Khmer page because you have specified the user one but let’s seehere in the android, now it is the “English page”, so you can do whatever you want withthe header.

It is the good way to specify that.

Lastly I want to talk about the error handler,here the error is like a generic one and if you want to say, okay now it’s a timeouterror, or maybe authentication error, so how to do that, so here it’s just the generalone, so you say, if error_instance of and you say timeout error, see how do I know that? Because there are many errors in volley, it’sgot the general one and the general one is from volley, okay, to get the error and thename of the class, what you have to do is, you just say com dot android dot volley andyou type error like this.

You see like AuthFailure error, VolleyError,NetworkError, NoConnectionError, so you can do the instance of this class, so it’s thebetter way to find out what is the error class, because this is the new thing and you mightnot find it on the internet.

So this is the right way to do that, so nowI want to test it, it is the NoConnection and TimeoutError and then you have a differenttoast in here, TimeoutError and else, if error instanceof NoConnectionError, same to others,NetworkError and the last one is ParseError.

So you are free to do like this, it’s notwrong because you just call it from the package name.

So, the parse error usually happen when thecall is just an object.

So, you can just type JSON Parse Error.

So let’s try it again, so now you get thedata, so if you want to test the error, you just go here (XAMPP) to the server and maybeyou just stop it like this and run it again and click see now “NoConnectionError”and if something is like Authentication and may be you want to test with the FTP, forexample, then you can have the password, username, then you can have the authentication failureand the server might be down and then this one is wrong the timeout when you connectit very long, longer than five seconds, then you will have a time out error.

Okay.

That’s it for the video.

Hope you enjoy it.

Thank you for watching and I will meet youin part 3.

Thank you!.



Thus Article Android Volley Tutorial (Android PHP MySQL) - How to Read/Send to Server (Part 2)

That article Android Volley Tutorial (Android PHP MySQL) - How to Read/Send to Server (Part 2) This time, hopefully we can give you all of the benefits. Well, I'll see you in another article post.

You now read the article Android Volley Tutorial (Android PHP MySQL) - How to Read/Send to Server (Part 2) the link address http://svedda79.blogspot.com/2016/12/android-volley-tutorial-android-php.html

1 Response to "Android Volley Tutorial (Android PHP MySQL) - How to Read/Send to Server (Part 2)"