åèä¸é¢ä»£ç å代ç ä¸ç注éå³å¯ï¼
PHP代ç ï¼
conn.phpæ¯è¿æ¥MySQLæ°æ®åºçã代ç å¦ä¸ï¼
<?php
$dbhost = "localhost:3306";
$dbuser = "root"; //æçç¨æ·å
$dbpass = ""; //æçå¯ç
$dbname = "testlogin"; //æçmysqlåºå
$cn = mysql_connect($dbhost,$dbuser,$dbpass) or die("connect error");
@mysql_select_db($dbname)or die("db error");
mysql_query("set names 'UTF-8'");
?>
login.php代ç ï¼
<?php
include ("conn.php");//è¿æ¥æ°æ®åº
$username=str_replace(" ","",$_POST['name']);//æ¥æ¶å®¢æ·ç«¯åæ¥çusernameï¼
$sql="select * from users where name='$username'";
$query=mysql_query($sql);
$rs = mysql_fetch_array($query);
if(is_array($rs)){
if($_POST['pwd']==$rs['password']){
echo "login succeed";
}else{
echo "error";
}
}
?>
class LoginHandler implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
//get username and password;
userName = user_name.getText().toString().trim();
password = pass_word.getText().toString().trim();
//è¿æ¥å°æå¡å¨çå°åï¼æçå¬çæ¯8080端å£
String connectURL="ç½ç«å°å/text0/com.light.text/login.php/";
//å¡«å
¥ç¨æ·åå¯ç åè¿æ¥å°å
boolean isLoginSucceed = gotoLogin(userName, password,connectURL);
//å¤æè¿åå¼æ¯å¦ä¸ºtrueï¼è¥æ¯çè¯å°±è·³å°ä¸»é¡µã
if(isLoginSucceed){
Intent intent = new Intent();
intent.setClass(getApplicationContext(), HomeActivity.class);
startActivity(intent);
proDialog.dismiss();
}else{
proDialog.dismiss();
// Toast.makeText(ClientActivity.this, "ç»å
¥é误", Toast.LENGTH_LONG).show();
System.out.println("ç»å
¥é误");
}
}
}
//ç»å
¥çæ¹æ³ï¼ä¼ å
¥ç¨æ· å¯ç åè¿æ¥å°å
private boolean gotoLogin(String userName, String password,String connectUrl) {
String result = null; //ç¨æ¥åå¾è¿åçStringï¼
boolean isLoginSucceed = false;
//test
System.out.println("username:"+userName);
System.out.println("password:"+password);
//åépost请æ±
HttpPost httpRequest = new HttpPost(connectUrl);
//Postè¿ä½ä¼ éåæ°å¿
é¡»ç¨NameValuePair[]éµåå¨å
List params = new ArrayList();
params.add(new BasicNameValuePair("name",userName));
params.add(new BasicNameValuePair("pwd",password));
try{
//ååºHTTP请æ±
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//åå¾HTTP response
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//è¥ç¶æç 为200å请æ±æåï¼åå°è¿åæ°æ®
if(httpResponse.getStatusLine().getStatusCode()==200){
//ååºå符串
result=EntityUtils.toString(httpResponse.getEntity());
ystem.out.println("result= "+result);
}
}catch(Exception e){
e.printStackTrace();
}
//å¤æè¿åçæ°æ®æ¯å¦ä¸ºphpä¸æåç»å
¥æ¯è¾åºç
if(result.equals("login succeed")){
isLoginSucceed = true;
}
return isLoginSucceed;
}