我有一个基本的身份验证表格现在工作。现在它只是在登录成功时发送一个字符串“成功”,不成功时发送“假”。当它成功时,我想返回用户数据,如UserID用于其他目的。我怎样才能做到这一点? 这里是我的PHP代码:Php到Android获取返回值
static public function Login($LoginID, $pass) {
$result = Db::getInstance()->getRow('
SELECT `UserID`
FROM `'._DB_PREFIX_.'cman_users`
WHERE `LoginID` = '.$LoginID.'
AND `password` = \''.$pass.'\'
');
//echo "User:Login ($LoginID)<br>";
if(isset($result['UserID']) && $result['UserID'] > 0)
return $result['UserID'];
else return 0;
}
这里是我的Android代码:
httpclient=new DefaultHttpClient();
// httppost= new HttpPost("http://127.0.0.1/API/check.php"); // make sure the url is correct.
//add your data
//httppost= new HttpPost("http://10.0.2.2:8080/API/check.php");
httppost = new HttpPost("http://cman.avaninfotech.com/api/cman/v1/checkLogin/");
nameValuePairs = new ArrayList<>(2);
nameValuePairs.add(new BasicNameValuePair("loginID",loginID.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("loginPass",loginPass.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
// tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("1")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "Login Success", Toast.LENGTH_SHORT).show();
}
});
startActivity(new Intent(MainActivity.this, HomePage.class));
}else{
showAlert();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}