Android 如何处理异步登录RESTAPI?

Android 如何处理异步登录RESTAPI?,android,api,rest,networking,login,Android,Api,Rest,Networking,Login,我正在尝试编写android应用程序的登录部分,该部分使用RESTAPI进行身份验证。这就是我目前拥有的 登录任务布尔函数 boolean LogInTask(String username, String password){ AuthOrNot.setCurrent(true); Log.wtf("START", "" + AuthOrNot.getCurrent()); UserAPI.Factory.getInstance().authenticat

我正在尝试编写android应用程序的登录部分,该部分使用RESTAPI进行身份验证。这就是我目前拥有的

登录任务布尔函数

   boolean LogInTask(String username, String password){



    AuthOrNot.setCurrent(true);
    Log.wtf("START", "" + AuthOrNot.getCurrent());

    UserAPI.Factory.getInstance().authenticateUser("testuser", "password").enqueue(new Callback<TokenJSON>() {
        @Override
        public void onResponse(Call<TokenJSON> call, Response<TokenJSON> response) {

            try{
                String token = response.body().getToken();
                Log.wtf("WORKS",""+ token);

                AuthOrNot.setCurrent(false);

                Log.wtf("AUTH", "" + AuthOrNot.getCurrent());


            }catch (NullPointerException t){
                Log.wtf("NO", "Didn't work, most likely incorrect username+password");
                AuthOrNot.setCurrent(false);
                t.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<TokenJSON> call, Throwable t) {
            Log.wtf("FAIL",""+t.getMessage());
            AuthOrNot.setCurrent(false);
        }
    });

    Log.wtf("AUTH", "" + AuthOrNot.getCurrent());

    return AuthOrNot.getCurrent();
}
布尔登录任务(字符串用户名、字符串密码){
AuthOrNot.setCurrent(真);
Log.wtf(“开始”,“AuthOrNot.getCurrent());
UserAPI.Factory.getInstance().authenticateUser(“testuser”,“password”).enqueue(新回调()命令){
@凌驾
公共void onResponse(调用、响应){
试一试{
字符串标记=response.body().getToken();
Log.wtf(“工作”和“+token”);
AuthOrNot.setCurrent(false);
Log.wtf(“AUTH”,“AuthOrNot.getCurrent());
}捕获(NullPointerException t){
Log.wtf(“否”,“不工作,很可能用户名+密码不正确”);
AuthOrNot.setCurrent(false);
t、 printStackTrace();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.wtf(“FAIL”和“+t.getMessage());
AuthOrNot.setCurrent(false);
}
});
Log.wtf(“AUTH”,“AuthOrNot.getCurrent());
返回AuthOrNot.getCurrent();
}

我知道这可能不是做这件事的最佳方式,这就是为什么我现在在这里。发生的情况是,网络请求需要1-2秒,此时代码已经位于底部,AuthoOrNot.getCurrent()==true,这意味着无论用户名+密码组合是否正确,都将启动下一个活动。

您可以创建一个接口将其用作回调。界面可能如下所示:

public Interface LoginCallback{
    void onLoginSuccess();
    void onLoginFailure();
}
void LogInTask(String username, String password, final LoginCallback callback){
// this isnt't needed: AuthOrNot.setCurrent(true);
//Log.wtf("START", "" + AuthOrNot.getCurrent());

UserAPI.Factory.getInstance().authenticateUser("testuser", "password").enqueue(new Callback<TokenJSON>() {
    @Override
    public void onResponse(Call<TokenJSON> call, Response<TokenJSON> response) {

        try{
            String token = response.body().getToken();
            Log.wtf("WORKS",""+ token);

            callback.onLoginSuccess();

            //Log.wtf("AUTH", "" + AuthOrNot.getCurrent());


        }catch (NullPointerException t){
            Log.wtf("NO", "Didn't work, most likely incorrect username+password");
            callback.onLoginFailure();
            t.printStackTrace();
        }
    }

    @Override
    public void onFailure(Call<TokenJSON> call, Throwable t) {
        Log.wtf("FAIL",""+t.getMessage());
        callback.onLoginFailure();
    }
});

//Log.wtf("AUTH", "" + AuthOrNot.getCurrent());

//nothing to return;
}
然后将代码更新为如下内容:

public Interface LoginCallback{
    void onLoginSuccess();
    void onLoginFailure();
}
void LogInTask(String username, String password, final LoginCallback callback){
// this isnt't needed: AuthOrNot.setCurrent(true);
//Log.wtf("START", "" + AuthOrNot.getCurrent());

UserAPI.Factory.getInstance().authenticateUser("testuser", "password").enqueue(new Callback<TokenJSON>() {
    @Override
    public void onResponse(Call<TokenJSON> call, Response<TokenJSON> response) {

        try{
            String token = response.body().getToken();
            Log.wtf("WORKS",""+ token);

            callback.onLoginSuccess();

            //Log.wtf("AUTH", "" + AuthOrNot.getCurrent());


        }catch (NullPointerException t){
            Log.wtf("NO", "Didn't work, most likely incorrect username+password");
            callback.onLoginFailure();
            t.printStackTrace();
        }
    }

    @Override
    public void onFailure(Call<TokenJSON> call, Throwable t) {
        Log.wtf("FAIL",""+t.getMessage());
        callback.onLoginFailure();
    }
});

//Log.wtf("AUTH", "" + AuthOrNot.getCurrent());

//nothing to return;
}

注意:方法名应该以小写开头,因此“LoginTask()”实际上应该是“LoginTask()”

您可以创建一个接口将其用作回调。界面可能如下所示:

public Interface LoginCallback{
    void onLoginSuccess();
    void onLoginFailure();
}
void LogInTask(String username, String password, final LoginCallback callback){
// this isnt't needed: AuthOrNot.setCurrent(true);
//Log.wtf("START", "" + AuthOrNot.getCurrent());

UserAPI.Factory.getInstance().authenticateUser("testuser", "password").enqueue(new Callback<TokenJSON>() {
    @Override
    public void onResponse(Call<TokenJSON> call, Response<TokenJSON> response) {

        try{
            String token = response.body().getToken();
            Log.wtf("WORKS",""+ token);

            callback.onLoginSuccess();

            //Log.wtf("AUTH", "" + AuthOrNot.getCurrent());


        }catch (NullPointerException t){
            Log.wtf("NO", "Didn't work, most likely incorrect username+password");
            callback.onLoginFailure();
            t.printStackTrace();
        }
    }

    @Override
    public void onFailure(Call<TokenJSON> call, Throwable t) {
        Log.wtf("FAIL",""+t.getMessage());
        callback.onLoginFailure();
    }
});

//Log.wtf("AUTH", "" + AuthOrNot.getCurrent());

//nothing to return;
}
然后将代码更新为如下内容:

public Interface LoginCallback{
    void onLoginSuccess();
    void onLoginFailure();
}
void LogInTask(String username, String password, final LoginCallback callback){
// this isnt't needed: AuthOrNot.setCurrent(true);
//Log.wtf("START", "" + AuthOrNot.getCurrent());

UserAPI.Factory.getInstance().authenticateUser("testuser", "password").enqueue(new Callback<TokenJSON>() {
    @Override
    public void onResponse(Call<TokenJSON> call, Response<TokenJSON> response) {

        try{
            String token = response.body().getToken();
            Log.wtf("WORKS",""+ token);

            callback.onLoginSuccess();

            //Log.wtf("AUTH", "" + AuthOrNot.getCurrent());


        }catch (NullPointerException t){
            Log.wtf("NO", "Didn't work, most likely incorrect username+password");
            callback.onLoginFailure();
            t.printStackTrace();
        }
    }

    @Override
    public void onFailure(Call<TokenJSON> call, Throwable t) {
        Log.wtf("FAIL",""+t.getMessage());
        callback.onLoginFailure();
    }
});

//Log.wtf("AUTH", "" + AuthOrNot.getCurrent());

//nothing to return;
}

注意:方法名称应该以小写开头,所以“LoginTask()”实际上应该是“LoginTask()”

这应该是对Daniel Chiriac的回答的评论,但因为我没有评论的名声:

由于必须重写在Refught2回调类中定义的onResponse方法,因此不能通过添加另一个参数来更改方法签名

我将使用“回调”作为LoginTask方法的方法参数

编辑:现在已更正:)


也可以在这里找到类定义:

这应该是对Daniel Chiriac的回答的评论,但由于我没有评论的声誉:

由于必须重写在Refught2回调类中定义的onResponse方法,因此不能通过添加另一个参数来更改方法签名

我将使用“回调”作为LoginTask方法的方法参数

编辑:现在已更正:)


还可以在此处找到类定义:

创建一个侦听器,以便在onresponse结束时被激发。在回调中执行cehck并转到其他活动这应该是什么类型的侦听器,内置方法之一还是我应该创建自己的侦听器?另外,“回调”的确切位置是什么?创建一个侦听器,以便在onresponse结束时被激发。在回调中执行cehck并转到其他活动这应该是什么类型的侦听器,内置方法之一还是我应该创建自己的侦听器?另外,“回调”在哪里?我是否需要添加回调作为LogInTask方法的第三个参数?对不起,是的,我本想添加它,但忘记了。我会修改我的答案,不用担心!一切都好了,非常感谢。我会标记“回答”哦,不用担心,我已经弄明白了,这就是我删除评论的原因。我应该写个条子说,我的错!谢谢,我是否需要添加回调作为LogInTask方法的第三个参数?对不起,是的,我本想添加它,但忘记了。我会修改我的答案,不用担心!一切都好了,非常感谢。我会标记“回答”哦,不用担心,我已经弄明白了,这就是我删除评论的原因。我应该写个条子说,我的错!谢谢你是的,我想把它加在那里。我不知道我把它添加到了错误的方法中。好地方。是的,我想把它加在那里。我不知道我把它添加到了错误的方法中。好地方。