【问题标题】:mLoggedInUser always null?mLoggedInUser 总是为空?
【发布时间】:2020-07-08 09:26:10
【问题描述】:

我正在调用方法 login 但它总是返回 login failed 并且 mLoggedInUser 总是 null(NullPointerException)。

如何在 mLoggedInUser 中传递响应。请帮忙!!!

public class LoginDataSource {

    private LoggedInUser mLoggedInUser;

    public Result<LoggedInUser> login(String username, String password) {

        try {
//            LoggedInUser fakeUser =
//                    new LoggedInUser(
//                            java.util.UUID.randomUUID().toString(),
//                            "Jane Doe");
            Call<LoggedInUser> loggedInUserCall = API.getLoginService().login(username, password);
            loggedInUserCall.enqueue(new Callback<LoggedInUser>() {
                @Override
                public void onResponse(Call<LoggedInUser> call, Response<LoggedInUser> response) {

                    mLoggedInUser = response.body();
                }

                @Override
                public void onFailure(Call<LoggedInUser> call, Throwable t) {

                }
            });
            if (mLoggedInUser != null)
                return new Result.Success<>(mLoggedInUser);
            return new Result.Error(new Exception("Login Failed"));
        } catch (Exception e) {
            return new Result.Error(new IOException("Error logging in", e));
        }
    }

    public void logout() {
        // TODO: revoke authentication
    }
}

【问题讨论】:

  • 它是一个异步调用,所以它总是 null 导致你从一个方法返回。您必须使用回调将登录结果返回给调用者。
  • 以及如何使用回调
  • 请帮我改正这段代码。

标签: android retrofit


【解决方案1】:

你应该创建这样的界面:

public interface NetworkResponseCallback {
    public void onSuccess(LoggedInUser user);
    public void onFailure(Throwable t);
}

然后编辑你的方法

    public class LoginDataSource {

    //private LoggedInUser mLoggedInUser;

    public void login(String username, String password , 
NetworkResponseCallback callback) {

        try {
//            LoggedInUser fakeUser =
//                    new LoggedInUser(
//                            java.util.UUID.randomUUID().toString(),
//                            "Jane Doe");
            Call<LoggedInUser> loggedInUserCall = 
     API.getLoginService().login(username, password);
            loggedInUserCall.enqueue(new Callback<LoggedInUser>() {
                @Override
                public void onResponse(Call<LoggedInUser> call, 
                                       Response<LoggedInUser> response) {

                    //mLoggedInUser = response.body();
                    if (callback != null)
                        callback.onSuccess(response.body());
                }

                @Override
                public void onFailure(Call<LoggedInUser> call, Throwable t) {
                    callback.onFailure(new Exception("Login Failed"));
                }
            });
//            if (mLoggedInUser != null)
//                return new Result.Success<>(mLoggedInUser);
//            return new Result.Error(new Exception("Login Failed"));
        } catch (Exception e) {
            return new Result.Error(new IOException("Error logging in", e));
        }
    }

    public void logout() {
        // TODO: revoke authentication
    }
}

【讨论】:

  • public Result login(String username, String password){ // 处理登录 Result result = dataSource.login(username, password, mResponseCallback); if (Result instanceof Result.Success) { setLoggedInUser(((Result.Success) result).getData()); } 返回结果; }
  • 它将如何返回响应
  • 结果类是用来保存结果的
猜你喜欢
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
相关资源
最近更新 更多