Android AWS DynamoDBMappingException:密钥的空值或空值

时间:2018-02-04 11:52:52

标签: android amazon-web-services amazon-dynamodb

我正在尝试AWS并在调用identityManager.getCachedUserID()后得到错误:

DynamoDBMappingException:key的空值或空值:public java.lang.String ***。NewsDO.getUserId()。

我使用默认示例新闻数据库,无法理解问题所在。

public class MainActivity extends AppCompatActivity {

DynamoDBMapper dynamoDBMapper;
IdentityManager identityManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
            getApplicationContext(), // Context
            "here is my pool", // Identity Pool ID
            Regions.AP_SOUTHEAST_1 // Region
    );

    AWSMobileClient.getInstance().initialize(this).execute();


    AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(credentialsProvider);
    this.dynamoDBMapper = DynamoDBMapper.builder()
            .dynamoDBClient(dynamoDBClient)
            .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
            .build();

    identityManager = new IdentityManager(getApplicationContext(), new AWSConfiguration(getApplicationContext()));
    IdentityManager.setDefaultIdentityManager(identityManager);

    createNews();
}

public void createNews() {
    final NewsDO newsItem = new NewsDO();

    newsItem.setUserId(identityManager.getCachedUserID());

    newsItem.setArticleId("Article1");
    newsItem.setContent("This is the article content");

    new Thread(new Runnable() {
        @Override
        public void run() {
            dynamoDBMapper.save(newsItem);
            // Item saved
        }
    }).start();
}

}

2 个答案:

答案 0 :(得分:0)

使用AWSMobileClient时,在调用initialize方法时,必须添加一个覆盖onComplete方法的回调对象。 AWSMobileClient根据CognitoCachingCredentialsProvider文件创建AWSConfigurationIdentityManager对象和awsconfiguration.json对象。唯一需要注意的是,您必须等到调用onComplete方法才能进行所有初始化。调用onComplete后,您现在可以安全地检索所需的信息。

由于IdentityManager是单身,AWSMobileClient在内部创建并设置它,因此您只需调用I dentityManager.getDefaultIdentityManager().getCachedUserID()即可检索身份ID。

我修改了代码以使其与AWSMobileClient一起使用。

String identityId = null;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AWSMobileClient.getInstance().initialize(this, new AWSStartupHandler() {
            @Override
            public void onComplete(AWSStartupResult awsStartupResult) {
                IdentityManager.getDefaultIdentityManager().getUserID(new IdentityHandler() {
                    @Override
                    public void onIdentityId(String s) {
                        System.out.println(s);
                        identityId = s;
                    }

                    @Override
                    public void handleError(Exception e) {
                        System.out.println(e);
                    }
                });
            }
        }).execute();
    }

    public void createNews() {
        final NewsDO newsItem = new NewsDO();

        newsItem.setUserId(identityId);
    }

答案 1 :(得分:0)

根据给定here的NoSQL数据库的AWS示例,在第二个活动的onCreate方法中重新初始化AWSMobileClient可能导致此错误消息:

例如,检查是否尚未在任何首次登录活动中创建AWSMobileClient的实例。