简体   繁体   中英

outer method execute before override inside method in java

Currently trying to set User u object by using JSON data from response.body() (retrofit). However, I am not able to do that. loginOperation(email,psswd) returns a boolean value which states whether a successful logged-in or not. When I try to execute this outer method, it returns check before the overridden method onResponse() .

Is there any advice? Thanks in advance!

AuthenticationCheck class ----------

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. If you want to block till response is received you need use synchronous call.execute

使用Rest Template代替您的呼叫,这将是被阻止的http呼叫,请参见此处的示例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM