我是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);
}
?>
**
答案 0 :(得分:0)
我不是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)&gt; 0表示有效匹配。参考http://php.net/manual/en/function.mysql-num-rows.php
记住
if ($result) {
}
除非您的查询错误,否则将始终为真。
答案 1 :(得分:0)
1)你可以发布方法'convertStreamToString'吗?
2)您是否检查了
echo json_encode($response);
返回正确的值并且不返回null?