Android HTTP POST (password security and response from server)
I'm working on an Android app, and my challenge is to "authentify" the user. All the users are memorized in a SLQ db (is the app on my开发者_如何学Python web site and I have some registered users memorized in a SQL database), and I was thinking to procede this way:
ask the user to enter his username and password, encrypt the password, send through POST the username and the password to a PHP class that queries the DB and sends back TRUE if the user and pass are correct and FALSE otherwise.
Browsing the stackoverflow questions I learned how to send data throw POST (see below) but I don't know how to receive a boolean value (TRUE if LOGGED IN or FALSE if LOGIN FAIL) from the server back to the Android App
Is there any other way to get the job done more easily or better?
Thank you! Marco :)
PS: this is the POST code pasted from an other question:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Http protocol aims at accessing page content (text) from remote servers. So php script should produce a message like "true" and "false", "0" and "1", etc. which you have to check in your application. You can use for example:
String res = new String(response.getData());
if (res.equals("true"))
;//logged
else
;//not logged
In any case what you're going to do is not secure. Imagine that I can capture your request to your server. Then I can simply send true to your application. Thus, your application will be authorized to do the things that you have for registered users.
If you are still working on this application read about authorization protocols.
精彩评论