简体   繁体   English

在Amazon Cognito Userpool for Android中检索用户属性(别名,必需和自定义)

[英]Retrieving user attributes(alias, required and custom) in amazon cognito userpool for android

The document below shows that we need to use GetDetailsHandler to retrieve user attributes for android. 以下文档显示我们需要使用GetDetailsHandler来检索android的用户属性。

http://docs.aws.amazon.com/cognito/latest/developerguide/tutorial-integrating-user-pools-android.html#tutorial-integrating-user-pools-user-details-android http://docs.aws.amazon.com/cognito/latest/developerguide/tutorial-integrating-user-pools-android.html#tutorial-integrating-user-pools-user-details-android

I've tried that and in onSuccess(), I did 我已经尝试过了,在onSuccess()中,

GetDetailsHandler getDetailsHandler = new GetDetailsHandler() {
        @Override
        public void onSuccess(CognitoUserDetails cognitoUserDetails) {
        Map userAtts = cognitoUserDetails.getAttributes().getAttributes();
        String userName =       userAtts.get("alias:preferred_username").toString();
        }

        @Override
        public void onFailure(Exception exception) {
       }
    };

username is still returned null. 用户名仍返回null。 I implemented it in onCreate(). 我在onCreate()中实现了它。 Am I doing it wrong? 我做错了吗? Any help is appreciated. 任何帮助表示赞赏。

If you are trying to access the value in the standard user attribute "preferred_username", then the key is just "preferred_username". 如果尝试访问标准用户属性“ preferred_username”中的值,则密钥仅为“ preferred_username”。
To access the preferred username try this: 要访问首选用户名,请尝试以下操作:

Map userAtts    = cognitoUserDetails.getAttributes().getAttributes();
String userName = userAtts.get("preferred_username").toString();

You have to initialize the map first after that you can use 您必须先初始化地图,然后才能使用

Map userAtts=new HashMap();
userAtts =cognitoUserDetails.getAttributes().getAttributes();
String userName = userAtts.get("alias:preferred_username").toString();

it works 有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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