我正在尝试构建一个可以与 Amazon DynamoDB 交互的简单 Android 应用程序.我已经下载了最新的 Android Studio 和 AWS 移动 SDK.我现在要做的只是在我的表客户"中插入一行.但是,当我运行该应用程序时,我遇到以下错误.如果你们能帮我看一下,我会非常感激,如果我犯了一些愚蠢的错误,我会很抱歉.(我已经按照文档设置识别池,创建新策略,将东西放入 AsyncTask..等)
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)
谢谢,
安
错误
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;
}
}
我终于在亚马逊支持的帮助下解决了这个问题
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.
结果我使用@DynamoDBIndexHashKey 而不是@DynamoDBHashKey!改正后就可以了!
So it turn out I use @DynamoDBIndexHashKey instead of @DynamoDBHashKey! After correcting that it works!
这篇关于基本操作中的 Android Amazon DynamoDb 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!