android - 在继续异步之前等待 retrofit2 完成的最佳方法

标签 android asynchronous runnable retrofit2 countdownlatch

我意识到有人问过类似的问题,但我是 android 的新手,发现答案有点困惑,因为它们的上下文略有不同。

我查看了 CountDownLatch 以及使用线程,但不确定使用哪种方法。任何帮助将非常感激。我也尝试过对 SharedPreferences 使用 apply() 而不是 commit() 。

我正在从 LoginActivity 进行 2 次 retrofit2 调用。我需要第一次调用的 token 才能在第二次调用中使用。我在第一次改造调用的 onResponse 方法中将 token 保存到 sharedpreferences 中的字符串。

在我的第二次调用中,serverToken 的值返回为应用程序之前运行时设置的 token

第一次调用(getToken)onResponse

   call.enqueue(new retrofit2.Callback<TokenResponse>() {    

            @Override
            public void onResponse(Call<TokenResponse> call, retrofit2.Response<TokenResponse> response) {

                if (response.isSuccessful()) {
                    TokenResponse tokenResponse = response.body();

                    LoginActivity.editor.putString("serverToken", tokenResponse.getAccessToken());
                    LoginActivity.editor.commit();

                } else {
                    Log.i("Server Token", "failed");
                }
            }
 }

登录 Activity

public class LoginActivity extends AppCompatActivity {

    public static SharedPreferences preferences;
    public static SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        authenticationController = new AuthenticationController();
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        editor = preferences.edit();
    }

    public void onLoginClicked(View view) {
        getToken();    //FIRST RETROFIT CALL
        connectToPush(); //SECOND CALL WHERE I NEED TOKEN FROM FIRST CALL
    }

    public void getToken() {
        authenticationController.login(grantType, username, password);
    }

    public void connectToPush() {
        authenticationController.connectToPush();
    }

我的第二次改造电话

public void connectToPush(){

  Log.i("sharedpreferencesToken", LoginActivity.preferences.getString("serverToken", "null serverToken"));

}

最佳答案

onResponse() 方法是一个回调接口(interface),简单地说,这意味着您可以从请求\事件中获取信息(您调用,它返回,因此回调) 并实现你想用它做什么(它是一个接口(interface),你实现它,因此 @Override 注释)。

这意味着:

  1. 您不需要 CountDownLatch,至少在这种情况下不需要,Retrofit2 会为您处理线程。

  2. 不需要 SharedPreferences,您可以直接从该回调中调用您想要的方法,因为信息在该实例中(除非您出于以下原因想要保存它下一个请求,请看下一个……)。

  3. 如果您想在本地存储该值,因为您以后需要它(或者以后用作自动登录的东西,您可以使用 SharedPreferences,但您不需要在该实例中从那里获取您的值 - 因为它存在于回调实例中(您将值保存在那里,再次从 Prefs 加载它是多余的,而响应包含可以简单传递的确切值。

所以:

call.enqueue(new retrofit2.Callback<TokenResponse>() {    

        @Override
        public void onResponse(Call<TokenResponse> call, retrofit2.Response<TokenResponse> response) {

            if (response.isSuccessful()) {
                TokenResponse tokenResponse = response.body();

                //right here you can call the other request and just give it the token
                connectToPush(tokenResponse);

                //if you really need to, save your value
                LoginActivity.editor.putString("serverToken", tokenResponse.getAccessToken());
                LoginActivity.editor.commit();

            } else {
                Log.i("Server Token", "failed");
            }
        }
 }

在你的第二个电话中:

public void connectToPush(TokenResponse tokenFromFirstRequest){

 //fire the next request using your token as a param!

}

关于android - 在继续异步之前等待 retrofit2 完成的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37445140/

相关文章:

android - ConstraintLayout 内容形状在滚动时改变

Android NFC,在onCreate 中做空检查?

javascript - 静态 Promise.resolve()/reject() 始终被解释为resolve()

javascript - 循环之间并行,循环内多个异步操作,如何实现?

java - 如何从可运行或类似的返回 boolean 值?

java - 如何告诉 Runnable 抛出 InterruptedException?

android - 如何在 google glass android 中读取二维码?

asynchronous - Angularjs bootstrap UI typeahead 无法异步工作

java - Android - 如何连续运行一个线程,一个接一个

java - OpenCV Java : how to find connected components of image in OpenCv JAVA