• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps [Urgent]Connecting MySQL by HTTPClient via PHP

ken717

Lurker
Hi everyone, I'm trying to create a login function in my new App.
I wrote a php file in my server to be an interface between my App and MySQL.

JAVA code
private void connectPHP(){
try {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("www.abc.com/login.php"); // I hide the actual URL for security reason.

ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", "user"));
parameters.add(new BasicNameValuePair("password", "pw"));

request.setEntity(new UrlEncodedFormEntity(parameters, "UTF_8"));
HttpResponse response = client.execute(request);
String responseMessage = response.getEntity().toString();
} catch (Exception e) {}
}
PHP code // To improve readability, I deleted irrelevant codes and simplify the remaining, aims to show what I'm going to do.
<?php
if ($_POST['username'] == 'user' && $_POST['password'] == 'pw')
echo 'Success';
else
echo 'Failed';
?>
I expect responseMessage will equal to "Success", but as result it returns a NULL value.

1. I'd try the php file in a web browser and it operates normally, I'm sure the problem came from the JAVA code.
2. I'd try to use the response.getStatusLine().getStatusCode() method to check the HTTP status code, but it doesn't return any number.

Please help !
 
Your first problem is that you are using PHP. :P

All kidding aside, your code looks fine to me, however I am no guru in PHP... With that said, You might try using print() instead of echo. Also, once you get things working correctly, you are going to want to be returning JSON from the server, so this tutorial might be of use for you. Connecting to MySQL database | Hello Android

I can personally confirm that the information in that article is accurate and works as intended.

If you don't want to ready your application for JSON yet, the only thing I can suggest is to break your server code into smaller pieces first. For example, start by simply echoing (or printing) a single statement and that's it. If that works, then move on by adding a single conditional for user. If that works, remove that conditional and try just checking password, if both of those work, then I don't know what to tell you lol. if at any stage something doesnt work, then that should give you a better idea of where to go next.


EDIT: It looks like in the article I linked, instead of doing
Code:
String msg = response.getEntity().toString();

They do:
Code:
InputStream is = response.getEntity().getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
       sb.append(line + "\n");
}
is.close();
String msg = sb.toString();

You might try that instead.


EDIT 2:

I ha almost forgotten that there is an API called EntityUtils That you can use to streamline this process, so instead of the above, you might do:

Code:
String msg = EntityUtils.toString(response.getEntity());
 
Thank you very very much for help :) !
I'd tried your suggested solution and the problem is now fixed !
So now I can continue my project LOL
Thanks again ! :)
 
Back
Top Bottom