I am trying to build a simple android app that can interact with Amazon DynamoDB. I have download the latest Android Studio and also AWS mobile SDK. What I am trying to do now is simply insert a row into my table "customer". However when I run the app I encounter the following error. Would really appreciate if you guys can help me take a look and sorry if I make some stupid mistakes.(I have follow the doc to set up identification pool, create new policy, put stuff in AsyncTask..etc)
Thanks,
An
Error
07-21 23:31:59.781 4207-4234/com.example.an.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.an.myapplication, PID: 4207
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMappingException: No interface com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBHashKey annotation found in class class com.example.an.myapplication.customers
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.needAutoGenerateAssignableKey(DynamoDBMapper.java:720)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(DynamoDBMapper.java:780)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(DynamoDBMapper.java:688)
at com.example.an.myapplication.MainActivity$db.doInBackground(MainActivity.java:67)
at com.example.an.myapplication.MainActivity$db.doInBackground(MainActivity.java:56)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
MainActivity.java
package com.example.an.myapplication;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.*;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.*;
public class MainActivity extends ActionBarActivity {
private CognitoCachingCredentialsProvider credentialsProvider;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
credentialsProvider = new CognitoCachingCredentialsProvider(
this.getApplicationContext(), /* get the context for the current activity */
"us-east-1:ef8bfc69-88b7-4714-ae0a-223f08a20fe5", // Identity Pool ID
Regions.US_EAST_1 // Region
);
new db().execute("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class db extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(credentialsProvider);
DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);
customers customer = new customers();
customer.setCustomerID("GG");
customer.setBalance(5);
mapper.save(customer);
return "Executed";
}
@Override
protected void onPostExecute(String result) {}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
}
customer.java
package com.example.an.myapplication;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.*;
@DynamoDBTable(tableName = "customer")
public class customers {
private String id;
private int balance;
@DynamoDBIndexHashKey(attributeName = "CustomerID")
public String getCustomerID() {
return id;
}
public void setCustomerID(String id) {
this.id = id;
}
@DynamoDBAttribute(attributeName = "Balance")
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
}