Practice English Speaking&Listening with: How to Connect Android with PHP and MySQL? Volley Tutorial (Part 2)

Difficulty: 0

Hello everyone, my name is Oum Saokosal.

This is part two of how to connect android with 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 to set up a volley library, how to make a singleton

class and how to do simple request.

In this video, Ill talk about how to send and read data from/into PHP and MySQL.

I will cover post and get.

I will talk about how to put parameters into the request and also I will talk about how

to do the HTTP header request and lastly I will talk about how to handle the errors.


This is the previous video, we could send two requests, this one is the single request1

and we call to

Kh is for Cambodia Khmer and second one is request to the local one, so make sure you

are running the XAMP and if you are running WAMP, its ok.

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

Here you see the second one to local, still it runs fast because the local should be faster

than the global one then it read and retrieve the data and then sends back to android.

This is how it works, so it is really a multiple thread.

Dont forget to put the internet here and also for the volley you have to put this into

the gradle.


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

So here like I said in the first video if you dont mention it, the first parameter

by default its a gap.

So now lets try to create a PHP page and test it, lets go to the htdocs and then

I have this one, and lets try to create a 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 call for an example, say It is get and here

echo It is post.

So for android, you have to specify the path here because I create the file its test and

then I create a string, number three now and the url3 and put the request here, you dont

have to delete this one because this is a separate code and just comment this one.

Its good to go now.


As we can see now, the return string is It is get, it means that if you dont specify

anything here, before one it is GET, so now I want to say, I want to call it to the POST,

what you have to do is, you put the

Okay, the post one and if you still want to use get, just say GET, because GET is the

default, so now you want to test again, again now you can see which is post, so now you

know how to send a data by GET or POST, if you are not sure what is POST you can go to

Google and find post vs. get, and this one explains it well from the w3shools here, so

GET request data from a specified resources and 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


Okay AND this one is okay to send the data but still you want to use the https, there

is no guarantee that host will send the data encrypted and so you have to use the https:ssl



But for android because you dont have any browser here, you just call it In-android

app, in website you have this and its about the bookmarking and some other like history

thing, but here in android these two are not so different, okay, but if you think that

maybe you want to put some data and then maybe you 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 its not POST, its a GET.

But if you want to send data by parameters without this, you have to think it is a POST,

so for me I prefer to use POST even though its the same thing but POST for me is always

the better.

Now we move on to parameter, so lets say now you want to use POST here and you say

if now lets say POST and you want to send a parameter and which is the key, maybe some

username and then maybe if username equals something, maybe just kosal, but I always

test if the key is existing or not, so in PHP you have isset and you say POST like this

and username, so make that the username, key is exists then you can compare like this and

then maybe you want to test the password as well, like this and the password is 123

string and then here you say correct, okay, and else incorrect.


Now you want to send a data here, to send a data you have to call by getParams but you

have to override this string request but in Java, now you have a better way to do that

overriding what you have to do is open the curly brace and close the curly brace after

the constructor like this, and then here, you say getParams and you can see now its

a getParams here and just double click it, now you have a nice getParams method, and

here is the hashmap, you want to declare a map like that and I say params and new Hashmap();

and params.put.

Now its a key, the key here is the same to this one, so its a username and the

second one is a value and what is the value now, its value is kosal right? and you want

to put second key and password and now you want to return params, okay, thats all,

now you can send the data to the PHP web server and then the data will read it and PHP will

test it, it if it is correct and it will echo correct if it incorrect and then echo incorrect.

Now you can see it is correct, now you can do something with the result now because it

is correct already.

Lets say you have this and I want to make something more interesting, I want to have

like a textbox and a passport textbox and a box here, we name it, so etusername, okay,

etpassword, this one is login, this one is login and if you give the correct username

and password and you click login, it should go to another page, so you should have another

activity here as well, Newgo to Activity here and you can use the navigation, lets stick

just stay with the basic one, and choose the hierarchy, the main one, Finish and into the

sub, I should have some text, lets say mainwelcome, so here in main, so I have to

get the data from the text so I have to clear the edit text, etpassword and around here

and for the username and password, it is like before

and then findViewById.

Now you want to call the event, Click button on the btnlogin, so here, btnlogin setOnClickListener,

new and space.

So you want to cut the whole code here, and pass it in here, okay, so now the problem

is the this, so we just got the get application context here.

Should be nice now, and you want to change kosal, number 123 here, the etusername.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 test the response here, so if response equal correct

but I dont want to use equal here because sometime you miss to leave a space after that

or maybe there is some space, like now we have 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, say and this one is more precise, okay, correct_login

here, else here, then you can go to the second activity here, intent in new intent (main

activity.this, subactivity.class) and you go to StartActivity and its now an error

or call it incorrect username and password.



Now lets try kosal again 123.

Ok now you know you can go to the password and

it says you log in with the wrong one.

Okay, now you can go to the subpage because the password and username is correct, lets

say if you login with the wrong one, okay there is some problem now, I got some problem

with the PHP, so I should change the code to this one, I should test the isset first

if the username and password is exists, like this step, then I can test like username equals

kosal and password equals 123.

It should be like this and in the real world actually you have to test again the database

but this one is just for example, I dont wanna go deep into the php, we are talking

about android and we should have a knowledge about this and if you dont know this maybe

you should watch another video of mine like how to do the login like this.


So now we test it again, if I test it like this and incorrect username password 123,

so now it goes to the subpage.

Okay, so now we move on to the header, so what is header?

Okay, if you dont know just headers, you just 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 you get the response and then you can get the

status line and headers and content, so usually you see the content you dont realise that

you have the header.

Header is something like this, you will see the 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 client request is from where?

Maybe from iPhone or from web browser but which web browser? you can see now its Mozilla.

May be he might be using Firefox.

This is the content and you can see the header and I recommend you read this but I will not

go down much I will explain a little bit about this, so what most people are interested with

http headers is about the User Agent, its a way that tests against the clients call,

by the way, which client call, which browser and which platform?

In this part lets say you test it in the PHP say SERVER [HTTP_USER_AGENT] and

the strstr, it means like try to find this IE6, inside user agent because user agent

has a long list like this and then you want to find a string of PHP, the PHP function

to find the string and then you call a server and user agent, remember if you dont know

this because you usually see the code like this, http, you see this the user dash agent

and then you want to call it in PHP, you call it 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 one or the English one, then you can rewrite to

this okay.

Some are examples about this and is about the content type and the charset, most people

get a problem with this because they dont put the charset as UTF_8, so they cannot get

the data from the API.


So, now lets test it.

Now we just use user agent only.

So where is user agent?

Here, In android you can just do it like that by overriding class here, you say get header,

see you get the header already, and it is the same thing because it is a map, so map<String,

String> and now I say header new HashMap like this and headers just put something and now

you want to put the user agent, okay, User dash Agent, and what is your agent now?

You can just tell PHP that my agent is android, so I would say MyAndroid, thats just an

example here and then I return the header.


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

Now, so maybe Im not interested much to this, so lets comment this, so just say

server and http underscore useragent like this 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 dont want to test it against

the correct login or whatever, so we need to comment this, now just Log.d (TAG, response)

is enough because we just want to see whether it 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 its a good way because you dont want to have a separate page, lets say for android

and for user web.

Lets say, here you go to localhost/customer/test.

PHP and now because you are using the general one, the code should be for the general purpose

but then if its for android, you can see now its an android code, maybe the charset

is different, maybe the image is different and the size for the image for android should

be smaller, okay, so this is the way that you want to test again the header and then

you can put some other things you want.

Okay, lets see the example here, now you want 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 back to here and quite similar, HTTP_ACCEPT_LANGUAGE,

it should be the same like this and English page else you give my page Khmer page.

So you can test it in here as well, okay so you can see now, its Khmer page because you

have specified the user one but lets see here in the android, now it is the English

page, so you can do whatever you want with the 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 its a timeout error, or maybe authentication error, so how

to do that, so here its just the general one, 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, its got the general one and the general one is

from volley, okay, to get the error and the name of the class, what you have to do is,

you just say com dot android dot volley and you type error like this.

You see like AuthFailure error, VolleyError, NetworkError, NoConnectionError, so you can

do the instance of this class, so its the better way to find out what is the error class,

because this is the new thing and you might not find it on the internet.

So this is the right way to do that, so now I want to test it, it is the NoConnection

and TimeoutError and then you have a different toast 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, its not wrong because you just call it from the package


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

So, you can just type JSON Parse Error.

So lets try it again, so now you get the data, so if you want to test the error, you

just go here (XAMPP) to the server and maybe you 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, for example, then you can have the password, username,

then you can have the authentication failure and the server might be down and then this

one is wrong the timeout when you connect it very long, longer than five seconds, then

you will have a time out error.


Thats it for the video.

Hope you enjoy it.

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

Thank you!

The Description of How to Connect Android with PHP and MySQL? Volley Tutorial (Part 2)