我有一個基本的身份驗證表格現在工作。現在它只是在登錄成功時發送一個字符串「成功」,不成功時發送「假」。當它成功時,我想返回用戶數據,如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();
}
}