我是Android开发的新手,我这里有一些我构建的代码,以便在我的移动应用程序中具有登录功能。我正在尝试将输入文本中输入的数据的值进行比较到 mysql 数据库中,但似乎我不明白重点,我真的不知道我的代码是否引导我进入某事,或者这只是胡说八道,你们能在这里帮我吗?顺便说一句,我没有收到任何错误,只是单击按钮后没有结果。
这是我的代码:
**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Edit Text
inputEmail = (EditText) findViewById(R.id.inputEmail);
inputPassword = (EditText) findViewById(R.id.inputPassword);
// Create button
Button btnSubmit = (Button) findViewById(R.id.btnLogin);
// button click event
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CheckLogin().execute();
}
});
}
class CheckLogin extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Main.this);
pDialog.setMessage("Logging in..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String eadd = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://10.0.2.2/TheCalling/log_in.php");
try {
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("eadd", eadd));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200){
entity = response.getEntity();
if(entity != null){
InputStream instream = entity.getContent();
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
String inputEmail = jsonResponse.getString("eadd");
String inputPassword = jsonResponse.getString("password");
if(eadd.equals(inputEmail) && password.equals(inputPassword)){
SharedPreferences sp = getSharedPreferences("logindetails", 0);
SharedPreferences.Editor spedit = sp.edit();
spedit.putString("eadd", eadd);
spedit.putString("password", password);
spedit.commit();
Toast.makeText(getBaseContext(), "SUCCESS!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "Invalid login details", Toast.LENGTH_SHORT).show();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
**
这是我的PHP文件:
**
<?php
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "sample_login";
$response = array();
$db_connect = mysql_connect($db_host, $db_user, $db_password) or die(mysql_error());
$db = mysql_select_db($db_name);
$eadd = $_POST['eadd'];
$password = $_POST['password'];
// mysql inserting a new row
$result = mysql_query("SELECT * FROM users WHERE eadd = '$eadd' and password = '$password'");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Login succes";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
?>
**
我不是Android开发人员,但这是我要说的。
-
php文件都很好。唯一的问题是,您没有返回电子邮件和密码,您似乎正在Android文件中访问这些电子邮件和密码。这里:
InputStream instream = entity.getContent(); JSONObject jsonResponse = new JSONObject(convertStreamToString(instream)); String inputEmail = jsonResponse.getString("eadd"); String inputPassword = jsonResponse.getString("password");
-
如果查询正确,则 mysql_query() 在成功时返回资源,如果查询正确,则返回 FALSE。您需要检查 mysql_num_rows($result)> 0 是否有效匹配。请参阅 http://php.net/manual/en/function.mysql-num-rows.php
记得
if ($result) {
}
将始终为真,除非您的查询是错误的。
1)你能发布方法'convertStreamToString'吗?
2)你检查过
echo json_encode($response);
返回正确的值并且不返回 null?