在java中覆盖内部方法之前执行外部方法 [英] outer method execute before override inside method in java

查看:50
本文介绍了在java中覆盖内部方法之前执行外部方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在尝试使用来自 response.body() 的 JSON 数据来设置 User u 对象(改造).但是,我无法做到这一点.loginOperation(email,psswd) 返回一个布尔值,表明是否成功登录.当我尝试执行此外部方法时,它会在覆盖方法 onResponse() 之前返回 check.

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 类 ----------

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

}

推荐答案

使用 Rest Template 而不是你的调用,这将是一个被阻止的 http 调用,参见 这里的例子

Use Rest Template instead of your call, it will be a blocked http call, see examples here

这篇关于在java中覆盖内部方法之前执行外部方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆