目前正尝试使用 response.body()(改装)中的JSON数据设置用户u 对象。但是,我无法做到这一点。 loginOperation(email,psswd)返回一个布尔值,指出是否成功登录。当我尝试执行此外部方法时,它会在重写方法 onResponse()之前返回 check 。
有什么建议吗?提前谢谢!
AuthenticationCheck类----------
public class AuthenticationCheck {
RetrofitConnection rc = new RetrofitConnection();
Retrofit retrofit = rc.getRetrofit();
private static boolean check = false;
private static User u = new User();
synchronized public boolean loginOperation(String email, String password){
LoginService loginService = retrofit.create(LoginService.class);
Call<ResponseBody> call = loginService.loginWithCredentials(new
LoginCredentials(email, password));
call.enqueue(new Callback<ResponseBody>() {
@Override
synchronized public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
check=true;
final JSONObject obj;
try {
obj = new JSONObject(response.body().string());
final JSONObject userObj = obj.getJSONObject("user");
u.setSurname(userObj.getString("surname"));
u.setPhone(userObj.getString("phonenumber"));
u.setUser_id(userObj.getInt("user_id"));
u.setName(userObj.getString("name"));
u.setEmail(userObj.getString("email"));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
synchronized public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("FAIL","onFailure ");
}
});
return check;
}
public User getAuthenticatedUser(){
return u;
}
LoginCredentials
public class LoginCredentials {
@SerializedName("email")
@Expose
private String email;
@SerializedName("password")
@Expose
private String password;
public LoginCredentials(String email, String password) {
this.email = email;
this.password = password;
}
}
LoginInterface
public interface LoginService {
@POST("mlogin")
Call<ResponseBody> loginWithCredentials(@Body LoginCredentials data);
}
用户类
public class User {
@SerializedName("user_id")
@Expose
private int user_id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("surname")
@Expose
private String surname;
@SerializedName("phonenumber")
@Expose
private String phone;
@SerializedName("email")
@Expose
private String email;
@SerializedName("password")
@Expose
private String password;
public User(int user_id, String name, String surname, String phone,String email,String pass){
this.user_id = user_id;
this.name = name;
this.surname = surname;
this.phone = phone;
this.email = email;
this.password = pass;
}
public User(){
this.user_id = 0;
this.name = null;
this.surname = null;
this.phone = null;
this.email = null;
this.password = null;
}
public String getEmail() { return email; }
public String getName() { return name; }
public String getPasswd() { return password; }
public String getPhone() { return phone; }
public String getSurname() { return surname; }
public int getUser_id() { return user_id; }
public void setEmail(String email) {
this.email = email;
}
public void setName(String name) {
this.name = name;
}
public void setPasswd(String password) {
this.password = password;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setUser_id(int user_id) { this.user_id = user_id; }
@Override
public String toString() {
return "Post{" +
"user_id='" + user_id + '\'' +
", name='" + name + '\'' +
", surname=" + surname +
", phone=" + phone +
", email='" + email + '\'' +
", pass=" + password +
'}';
}
}
答案 0 :(得分:0)
call.enqueue是一个异步操作,因此在此调用之后loginOperation()中的其余代码将继续执行。如果要阻止直到收到响应,则需要使用同步call.execute
答案 1 :(得分:0)
使用“休息模板”代替您的通话,它将是一个已阻止的http通话,请参阅examples here