gpt4 book ai didi

java - 为什么我的 'userID' 初始化为 0 而不是从 PHP 发送的内容?

转载 作者:行者123 更新时间:2023-11-29 18:31:44 26 4
gpt4 key购买 nike

我试图将 PHP 后端中标记为“sno”的 userId 放入共享首选项中,然后在尝试根据该用户的 userID 加载帖子时从共享首选项中检索 userId。调试器中的 userId 显示为“0”。但根据调试器,所有其他信息(例如电子邮件名称和唯一 ID)都很好。

这是我的登录过程的请求对象,该过程应该将用户 ID 存储在首选项中。

RequestInterface requestInterface =             retrofit.create(RequestInterface.class);
User user = new User();
user.setEmail(email);
user.setPassword(password);
ServerRequest request = new ServerRequest();
request.setOperation(Constants.LOGIN_OPERATION);
request.setUser(user);
Call<ServerResponse> response = requestInterface.operation(request);
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse resp = response.body();
Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
if(resp.getResult().equals(Constants.SUCCESS)){
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.IS_LOGGED_IN,true); editor.putString(Constants.EMAIL,resp.getUser().getEmail()); editor.putString(Constants.NAME,resp.getUser().getName()); editor.putString(Constants.UNIQUE_ID,resp.getUser().getUnique_id());
editor.putInt(Constants.USER_ID, resp.getUser().getUserID());
editor.apply();
goToAllPosts();
}
progress.setVisibility(View.INVISIBLE);
}

这是我的用户类:公共(public)类用户{

private String name;
private String email;
private String unique_id;
private String password;
private String old_password;
private String new_password;
private int userID;
private List<Post> allPostsList;
public List<Post> getAllPosts(){
return allPostsList;
}
public void setAllPosts(List<Post> allPostsList){
this.allPostsList = allPostsList;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getUnique_id() {
return unique_id;
}
public int getUserID(){
return userID;
}
public int setUserID(int userID) { return this.userID = userID;}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setPassword(String password) {
this.password = password;
}
public void setOld_password(String old_password) {
this.old_password = old_password;
}
public void setNew_password(String new_password) {
this.new_password = new_password;
}
}

这是我的相关 php:索引.php:

if ($operation == 'login') {
if(isset($data -> user ) && !empty($data -> user) && isset($data -> user -> email) && isset($data -> user -> password)){
$user = $data -> user;
$email = $user -> email;
$password = $user -> password;
echo $fun -> loginUser($email, $password);
} else {
echo $fun -> getMsgInvalidParam();
}

函数.php:

public function loginUser($email, $password) {
$db = $this -> db;
if (!empty($email) && !empty($password)) {
if ($db -> checkUserExist($email)) {
$result = $db -> checkLogin($email, $password);
if(!$result) {
$response["result"] = "failure";
$response["message"] = "Invaild Login Credentials";
return json_encode($response);
} else {
$response["result"] = "success";
$response["message"] = "Login Successful";
$response["user"] = $result;
return json_encode($response);
}
} else {
$response["result"] = "failure";
$response["message"] = "Invaild Login Credentials";
return json_encode($response);
}
} else {
return $this -> getMsgParamNotEmpty();
}
}

DBOperations.php:

public function checkLogin($email, $password) {
$sql = 'SELECT * FROM users WHERE email = :email';
$query = $this -> conn -> prepare($sql);
$query -> execute(array(':email' => $email));
$data = $query -> fetchObject();
$salt = $data -> salt;
$db_encrypted_password = $data -> encrypted_password;
if ($this -> verifyHash($password.$salt,$db_encrypted_password) ) {
$user["name"] = $data -> name;
$user["email"] = $data -> email;
$user["unique_id"] = $data -> unique_id;
$user["sno"] = $data -> sno;
return $user;
} else {
return false;
}
}
public function checkUserExist($email){
$sql = 'SELECT COUNT(*) from users WHERE email =:email';
$query = $this -> conn -> prepare($sql);
$query -> execute(array('email' => $email));
if($query){
$row_count = $query -> fetchColumn();
if ($row_count == 0){
return false;
} else {
return true;
}
} else {
return false;
}
}

如果我没有包含一些必要的建议,请告诉我,我可以发布它。提前致谢

最佳答案

User.userID 持有 0,因为 0int 的默认值,并且是一个真实值无法从您的网络响应中反序列化。仔细检查您的服务器响应是否使用 "userID" 作为保存此 ID 的字段名称,而不是类似 "user_id" 的名称。

关于java - 为什么我的 'userID' 初始化为 0 而不是从 PHP 发送的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com