我正在尝试使用我的Android应用进行登录,但是我对php和mySQL非常不熟悉,需要一些帮助。

我已经创建了寄存器,它成功将用户的数据上传到数据库中。尽管当我尝试调用LoginRequest时,它并没有告诉我登录是成功还是失败。我认为这是因为我的responseListener或php文件出了点​​问题。

这是我的Login.php(我已经检查了主机,用户密码和数据库)

<?php
    $con = mysqli_connect("host", "user", "password", "database");

    $email = $_POST["email"];
    $password = $_POST["password"];

    $statement = mysqli_prepare($con, "SELECT * FROM user WHERE email = ? AND password = ?");
    mysqli_stmt_bind_param($statement, "ss", $email, $password);
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $user_id, $username, $email, $password);

    $response = array();
    $response["success"] = false;

    while(mysqli_stmt_fetch($statement)){
        $response["success"] = true;
        $response["username"] = $username;
        $response["email"] = $email;
        $response["password"] = $password;
    }

    echo json_encode($response);
?>


这是我的LoginRequest类(具有所有必需的导入)

public class LoginRequest extends StringRequest {
    private static final String LOGIN_REQUEST_URL = ".../Login.php";
    private Map<String, String> params;

    public LoginRequest(String email, String password, Response.Listener<String> listener) {
        super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
        params = new HashMap<String, String>();
        params.put("email", email);
        params.put("password", password);
    }

    public Map<String, String> getParams() {
        return params;
    }
}


这是我称为LoginRequest的地方。我没有运行时异常或编译器异常。

final Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) { //problem is here, I did some debugging and it turns out this doesn't get called
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        boolean success = jsonObject.getBoolean("success");
                        if(success) {
                            System.out.println("success");
                        } else {
                            System.out.println("you failed me for the last time");
                        }
                    } catch(JSONException e) {
                        System.out.println("error");
                        e.printStackTrace();
                    }
                }
            };
            LoginRequest loginRequest = new LoginRequest(user.getEmail(), user.getPassword(), responseListener);


最后,这是我数据库中名为用户的表

1   user_idPrimary  int(11)         No  None        AUTO_INCREMENT   Change
2   username    varchar(16) utf8_unicode_ci     No  None             Change
3   email   varchar(30) utf8_unicode_ci     No  None             Change
4   password    varchar(16) utf8_unicode_ci     No  None             Change


对于冗长的代码,我深表歉意。有谁知道出什么问题了吗?

谢谢!

最佳答案

 LoginRequest loginRequest = new LoginRequest(user.getEmail(), user.getPassword(), responseListener);


每个请求都必须添加到您的请求单例队列中。您从未添加。

实例化RequestQueue。

RequestQueue queue = Volley.newRequestQueue(this);


然后将请求添加到RequestQueue。

 queue.add(stringRequest);

10-04 11:03