我是 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);
}
?>
**