CognitoUserPool userPool =新的CognitoUserPool(this,AWSMobileClient.getInstance()。getConfiguration());给空值

时间:2019-02-21 21:12:50

标签: android amazon-web-services amazon-cognito aws-mobilehub

我正在使用AWS Cognito进行登录。我不想对userPoolId,clientId,clientSecret和region的值进行硬编码。我找到了使用

的方法

CognitoUserPool userPool =新的CognitoUserPool(this,AWSMobileClient.getInstance()。getConfiguration());

但是userPool提供的是空值。

awsconfiguration.json放置在res / raw / awsconfiguration.json

谢谢您的帮助!

2 个答案:

答案 0 :(得分:0)

要详细说明Karthikeyan的评论,您必须确保确实对AWSMobileClient进行了初始化,然后才能使用它来获取AWS Configuration。以下代码段演示了实现所需内容的一种方法:

    CognitoUserPool cup;
    final CountDownLatch latch = new CountDownLatch(1);

    AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {

                @Override
                public void onResult(UserStateDetails userStateDetails) {
                    Log.i("INIT", "onResult: " + userStateDetails.getUserState());
                    latch.countDown();
                }

                @Override
                public void onError(Exception e) {
                    Log.e("INIT", "Initialization error.", e);
                    latch.countDown();
                }
            }
    );

    try {
        latch.await();
        cup = new CognitoUserPool(getApplicationContext(), AWSMobileClient.getInstance().getConfiguration());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

希望有帮助!

答案 1 :(得分:0)

根据文档

https://aws-amplify.github.io/docs/sdk/android/authentication

您需要进入MainActivity并在onCreate()内运行initialize()例程:

AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {

        @Override
        public void onResult(UserStateDetails userStateDetails) {
            Log.i("INIT", "onResult: " + userStateDetails.getUserState());
        }

        @Override
        public void onError(Exception e) {
            Log.e("INIT", "Initialization error.", e);
        }
    }
);

由于您尚未登录,它将显示SIGNED_OUT状态。