[英]outer method execute before override inside method in java
Currently trying to set User u object by using JSON data from response.body() (retrofit). 当前尝试通过使用来自response.body()的 JSON数据设置User u对象(改进)。 However, I am not able to do that.
但是,我无法做到这一点。 loginOperation(email,psswd) returns a boolean value which states whether a successful logged-in or not.
loginOperation(email,psswd)返回一个布尔值,该值指示是否成功登录。 When I try to execute this outer method, it returns check before the overridden method onResponse() .
当我尝试执行此外部方法时,它在覆盖的方法onResponse()之前返回check 。
Is there any advice? 有什么建议吗? Thanks in advance!
提前致谢!
AuthenticationCheck class ---------- 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);
}
User Class 用户类别
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 +
'}';
}
}
call.enqueue is an asynchronous operation, so rest of the code in loginOperation() after this call will continue to execute. call.enqueue是一个异步操作,因此此调用之后loginOperation()中的其余代码将继续执行。 If you want to block till response is received you need use synchronous call.execute
如果要阻塞直到收到响应,则需要使用同步调用。
使用Rest Template代替您的呼叫,这将是被阻止的http呼叫,请参见此处的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.