簡體   English   中英

在Java中重寫內部方法之前執行外部方法

[英]outer method execute before override inside method in java

當前嘗試通過使用來自response.body()的 JSON數據設置User 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;
}

登錄信息

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;
}

}

登錄界面

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 +
            '}';
}

}

call.enqueue是一個異步操作,因此此調用之后loginOperation()中的其余代碼將繼續執行。 如果要阻塞直到收到響應,則需要使用同步調用。

使用Rest Template代替您的呼叫,這將是被阻止的http呼叫,請參見此處的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM