简体   繁体   中英

AsyncTask doInBackground method is not calling

I am using cognitouserpool codes inside AsynTask class's doInBackground method. But my method is not calling. I have tried by checking it with a Log. It is not working.

AsyncTask

    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());
            ...
        }
    }

AnotherClass.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();
    }
}

But the Log is not showing. Am I doing anything wrong in my coding. I exactly need the token to pass as the header while doing a POST call to AWS server. But it returns null because doInBackground() is not working. Even the Log in the TokenAuthenticator class is also not woking

You are not actually printing the log inside doInBackground method. You are trying to print it in callback method when token fetch is complete. I'm assuming your callback was never invoked which is actually printing the logs.

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

To verify what I'm saying print add new a Log statement outside and you should see logs getting printed in console. Something like :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM