0

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;
}
}

2 Answers 2

2

I finally fix the problem with the help of Amazon support

In customers.java, you don't have an attribute annotated with DynamoDBHashKey.
Not sure how your DynamoDB table is set up. At minimum, it needs a hash key.
If CustomerID is the hash key, then you need to change of the annotation of id
from DynamoDBIndexHashKey to DynamoDBHashKey.

So it turn out I use @DynamoDBIndexHashKey instead of @DynamoDBHashKey! After correcting that it works!

Sign up to request clarification or add additional context in comments.

Comments

1

I had this problem today and I solved it by adding -keep public class your_class in proguard-rules.pro.

I don't think, that it is a right way to solve it, but it works for me.

http://developer.android.com/tools/help/proguard.html#configuring

7 Comments

Do you mean adding "-keep public class <customers>" into the proguard-rules.pro? I tried it and it didnt work for me. Still give me the same error message
After adding "-keep public class customers" into the proguard-rules.pro. I have the same error with a slight differece in this line. 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
Opps sorry just realize that is the same error. So apparently add stuff in proguard-rules.pro doesn't really change anything..
An Lin. Sorry. I tried it yesterday and it worked.. I don't know, what exactly happened yesterday, but today it doesn't work again. You're right..
Nynuzoud still appreciate the reply! I tried many times on the sample project provided by Amazon link but their project are all outdated and I cant even run it on the latest android studio. This is kind of frustrating, I am pretty sure I follow all their steps. ( I tried Cognito Sync and it actually work, so this must be a dynamoDB problem...)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.