requestQueue = Volley.newRequestQueue(this);
sign_in_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
request = new StringRequest(Request.Method.POST, "http://benedict.reconlasertag.sg/user_control.php", new Response.Listener<String>() {
//response on sever
@Override
public void onResponse(String response) {
try {
String user = mEmailView.getText().toString();
String pass = mPasswordView.getText().toString();
JSONObject jsonObjectzz = null;
jsonObjectzz = new JSONObject(response);
System.out.println("@@@@@ here is the final jsonobject" + jsonObjectzz);
if (jsonObjectzz.names().get(0).equals("success")) {
Toast.makeText(getApplicationContext(), "SUCCESS" + jsonObjectzz.getString("success"), Toast.LENGTH_SHORT).show();
Login.username = jsonObjectzz.getString("tryme");
startActivity(new Intent(getApplicationContext(), Welcome.class));
} else {
Toast.makeText(getApplicationContext(), "Error" + jsonObjectzz.getString("error"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
Login.useremail = mEmailView.getText().toString();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("email", mEmailView.getText().toString());
hashMap.put("password", mPasswordView.getText().toString());
return hashMap;
}
};
requestQueue.add(request);
}
});
以上是我的LOGIN SYSTEM代码文件的摘录。但是,在 POSTMAN 上测试时,来自 jsonObject 的响应与 PHP URL 给出的响应不匹配。相反,我的 android 应用程序上显示的响应显示了在我更新新信息之前使用的结果。这对我来说没有任何意义。
接下来是从 php 文件中提取的内容
class User {
private $db;
private $connection;
function __construct() {
$this -> db = new DB_Connection();
$this -> connection = $this->db->getConnection();
}
public function does_user_exist($email,$password)
{
$query = "Select * from users where email='$email' and password = '$password' ";
$result = mysqli_query($this->connection, $query);
//$resultb = mysql_query($query);
if(mysqli_num_rows($result)>0){
$success = " There are some rows found";
$row = $result->fetch_assoc();
$r = $row["name"];
//$row = mysql_fetch_assoc($result);
$json['success'] = ' Welcome To Earth'. $r;
$json['tryme'] = $r;
header('Content-type: application/json');
echo json_encode($json);
mysqli_close($this -> connection);
}
else{
$query = "Insert into users (email, password) values ( '$email','$password')";
$inserted = mysqli_query($this -> connection, $query);
if($inserted == 1 ){
$json['success'] = ' Acount created';
}else{
$json['error'] = ' Wrong password';
}
header('Content-type: application/json');
echo json_encode($json);
mysqli_close($this->connection);
}
}
}
$user = new User();
if(isset($_POST['email'],$_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
// if(isset($_GET["a"])) echo "a is set\n";
//$name = $_GET['name'];
if(!empty($email) && !empty($password)){
$encrypted_password = md5($password);
$user-> does_user_exist($email,$password);
}else{
echo json_encode("you must type both inputs");
}
}
这是使用 POSTMAN 测试时给出的结果:
{“成功”:“欢迎来到 EarthBen”,“tryme”:“Ben”}
同样,Android 提取的结果是 System.out.println("@@@@@ here is the final jsonobject" + jsonObjectzz) 是
@@@@@ 这里是最终的 jsonobject{"success":" Welcome ","tryme":null}