【问题标题】:AsyncTask doInBackground method is not callingAsyncTask doInBackground 方法未调用
【发布时间】:2020-01-20 16:34:37
【问题描述】:

我在 AsynTask 类的 doInBackground 方法中使用 cognitouserpool 代码。但是我的方法没有调用。我试过用日志检查它。它不工作。

异步任务

    public class AWSInitiator extends AsyncTask<Void, Void, Void> {
        private Context context;
        public AWSInitiator(Context context) {
            this.context = context;
        }
        @Override
        protected Void doInBackground(Void... voids) {
            CognitoUserPool.......
        Log.i("ACCESS_TOKEN", "Test");
        AuthenticationHandler authenticationHandler = new AuthenticationHandler() {
            Log.i("ACCESS_TOKEN", userSession.getIdToken().getJWTToken());
            ...
        }
    }

另一个类.java

public class TokenAuthenticator implements Authenticator {
    @Override
    public Request authenticate(Route route, Response response) {
        Log.i("Authenticate", "auth");
    AWSInitiator awsInitiator = new AWSInitiator(AppSetting.getInstance().getApplicationContext());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        awsInitiator.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
         awsInitiator.execute();
    }
return response.request().newBuilder()
                .header("Authorization", "Bearer " + AppSetting.getInstance().getSDKDataManager().getAccessToken())
                .build();
    }
}

但是日志没有显示。我在编码中做错了什么吗?在对 AWS 服务器进行 POST 调用时,我确实需要将令牌作为标头传递。但它返回 null 因为 doInBackground() 不起作用。就连 TokenAuthenticator 类中的 Log 也不行

【问题讨论】:

    标签: java android android-asynctask


    【解决方案1】:

    您实际上并没有在 doInBackground 方法中打印日志。当令牌获取完成时,您正尝试在回调方法中打印它。我假设您的回调从未被调用过,实际上是在打印日志。

    Log.i("ACCESS_TOKEN", userSession.getIdToken().getJWTToken());
    

    为了验证我所说的 print 在外面添加一个新的 Log 语句,您应该会看到日志在控制台中打印出来。类似的东西:

    Log.i("ACCESS_TOKEN", "Test logs");
    

    【讨论】:

    • 你是对的。我检查了外部回调方法。即使这样也行不通
    • 我已经编辑了我的问题。请参考我的问题中的 TokenAuthenticator 类
    • 我认为你应该把你的问题分成两部分 1. 在你的异步任务上调用 execute/executeOnExecutor 并检查“doInBackground”是否被调用。您可以通过放置断点或将日志放在回调方法之外来做到这一点。 2. 在你确认 doInBackground 方法被调用后,调试你的验证器类。异步任务非常简单,使用您提供的代码它应该可以工作并且应该调用“doinBackground”。我认为您的身份验证器类有问题。
    • 我已经检查过“doinBackground”没有被调用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2017-03-07
    • 1970-01-01
    相关资源
    最近更新 更多