无法验证
问题描述:
此时,我希望用户输入他/她的用户名和密码(数据被模拟@MySQL) 但它不起作用。无法验证
我file.php:
<?php
include("ConnectDatabase.php");
$Username = $_POST['Username'];
$Password = $_POST['Password'];
$q = mysql_query("SELECT Username, Password FROM Users
where Username = '".$Username."' and
Password = '".$Password."'");
if(mysql_num_rows($q) > 0){
print json_encode($q);
}else{
print "1";
}
mysql_close();
?>
我的Java文件:
public class Authentication extends Activity implements OnClickListener,
OnKeyListener, OnCheckedChangeListener {
/** Called when the activity is first created. */
ArrayList<NameValuePair> authentication;
String passIn, userIn, result;
EditText username, password;
CheckBox remember;
Button b_login;
InputMethodManager inputManager;
InputStream is;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// userIn = username.getText().toString();
// passIn = password.getText().toString();
username = (EditText) findViewById(R.id.usrname);
password = (EditText) findViewById(R.id.password);
inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
remember = (CheckBox) findViewById(R.id.remember);
remember.setOnCheckedChangeListener(this);
b_login = (Button) findViewById(R.id.login);
b_login.setOnClickListener(this);
username = (EditText) findViewById(R.id.usrname);
username.setOnKeyListener(this);
password = (EditText) findViewById(R.id.password);
password.setOnKeyListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
while (true) {
userIn = username.getText().toString();
passIn = password.getText().toString();
try {
sendAuthenticationData(userIn, passIn);
break;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WrongInputException e) {
Toast.makeText(Authentication.this, "Invalid username or password", Toast.LENGTH_LONG);
}
// break;
}
Intent intent = new Intent(this, ApplicationMenus.class);
this.startActivity(intent);
clearText(username,password);
break;
}
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
public void clearText(EditText usr, EditText pass) {
usr.setText("");
pass.setText("");
}
public void sendAuthenticationData(String username, String password)
throws ClientProtocolException, IOException, WrongInputException {
authentication = new ArrayList<NameValuePair>();
authentication.add(new BasicNameValuePair("Username", userIn));
authentication.add(new BasicNameValuePair("Password", passIn));
this.sendData(authentication);
}
public void sendData(ArrayList<NameValuePair> data)
throws ClientProtocolException, IOException, WrongInputException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"path/Authentication.php"); // I use real path here
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// is = entity.getContent();
String temp = EntityUtils.toString(entity);
if (temp.equals("1")) {
throw new WrongInputException();
}
}}
这是我第一次写的是用PHP服务器连接代码,和JSON。我非常迷惑JSON,尽管我试图遵循很多样本。欣赏任何帮助
答
您忘记的第一件事就是mysql_real_escape_string
用于传入变量:
$Username = $_POST['Username'];
而且你的第二个问题是有可能的:
if (mysql_num_rows($q) > 0) {
print json_encode($q);
的$q
变量是mysql_结果处理。它不能以JSON为代表。您可能想要使用:
print json_encode(mysql_fetch_assoc($q));
因此,您至少会得到一个带有用户名/密码的结果数组/对象。如果你只是发送另一个简单的数字结果 - print "2";
什么的,也许会更容易。
你还检查了你是否运行PHP 5.2或更高版本,以便json_encode
肯定可用?
这是什么问题?另外,你有没有得到任何错误?你的应用崩溃了吗?如果是这样,你可以粘贴logcat trace吗? – Cristian
我只是不知道为什么它不起作用。我没有得到任何错误。 – Yoo
调查PDO的准备报表,因为你的代码不安全=> http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/ – Alfred