Reputation: 157
I'm currently just trying to add an item to a NoSQL DynamoDB that I set up, I've been following the docs from Amazon on how to do this but it never works correctly and spits back errors.
I found this guide for using documents to do CRUD operations on a dynamoDB: https://aws.amazon.com/blogs/mobile/using-amazon-dynamodb-document-api-with-the-aws-mobile-sdk-for-android-part-2/ (that's part 2, part 1 talks about using documents more than connecting).
At first I tried it in my main activity but swiftly learned that running network operations on the main is prohibited with Android (which makes complete sense). So I put it in a class which extends ASyncTask. That looks like this:
public class DBControl_v2 extends AsyncTask<String, Void, String> {
CognitoCachingCredentialsProvider credentialsProvider;
AmazonDynamoDBClient dbClient;
Table dbTable;
Context context;
protected String doInBackground(String... items){
System.out.println(items);
// Create a new credentials provider
credentialsProvider = new CognitoCachingCredentialsProvider(
context, "us-east-2_---------", Regions.AP_NORTHEAST_2);
// Create a connection to DynamoDB
dbClient = new AmazonDynamoDBClient(credentialsProvider);
// Create a table reference
dbTable = Table.loadTable(dbClient, "ShoppingLists");
Document memo = new Document();
memo.put("Apple", "apple");
dbTable.putItem(memo);
But the issue is the "context" for the credential provider, the constructor needs this and as far as I can tell it has to be from a class that extends an activity but honestly I don't know and in the guide I was following it just says "context" which is very unhelpful.
The error from logcat when running this is:
java.lang.IllegalArgumentException: context can't be null
Which makes perfect sense since context is null but if I try to initialize it, it creates a huge thing and I can't imagine that's the way to do it.
I created another class which is exactly the same except it extends "Activity" but when I run that it spits back this error:
Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
Which probably means you can't just extend activity and expect it to work correctly.
Initially I was using this guide (from Amazon directly): https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-nosql-database.html
But got hung up when it required an "identityManager" object because it doesn't say before that in the tutorial what that even is. When I look up the whitepaper on IdentityManager, the constructor requires that damn context as well. Is context something frequently used with Android, maybe since I have little Android experience that's why this has become such an issue.
Any help where to go from here would be appreciated.
Upvotes: 1
Views: 3194
Reputation: 225
If you are using this guide that you referenced, the first thing you should make sure is that you have added the AWSMobile client in the OnCreate() of you MainActivity. It's a singleton object instance that manages your connection. The code is at this link https://docs.aws.amazon.com/aws-mobile/latest/developerguide/getting-started.html#add-aws-mobile-sdk-basic-setup but I have also pasted it here:
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper;
public class MainActivity extends AppCompatActivity {
// Declare a DynamoDBMapper object
DynamoDBMapper dynamoDBMapper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate a AmazonDynamoDBMapperClient
AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(AWSMobileClient.getInstance().getCredentialsProvider());
this.dynamoDBMapper = DynamoDBMapper.builder()
.dynamoDBClient(dynamoDBClient)
.awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
.build();
}
} `
Then you should be able to use the code noted at https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-nosql-database.html like noted here:
Runnable runnable = new Runnable() {
public void run() {
dbClient = new AmazonDynamoDBClient(credentialsProvider);
// Create a table reference
dbTable = Table.loadTable(dbClient, "ShoppingLists");
Document memo = new Document();
memo.put("Apple", "apple");
dbTable.putItem(memo);
}
};
Thread mythread = new Thread(runnable);
mythread.start();
Multiple ways to make the call above, you can also adjust based upon this to use your current code. Remember this guide assumes that you have created your NoSQL database and starter project using AWS Mobile Hub, if you have not, no worries, just make sure you follow the SDK Setup info here: https://docs.aws.amazon.com/aws-mobile/latest/developerguide/how-to-android-sdk-setup.html
Upvotes: 1