在DynamoDB中存储用户的姓名和姓氏

时间:2016-11-09 11:23:34

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

尽管在互联网上关注亚马逊文档和各种教程,但我还是无法弄清楚这些错误:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                  Process: com.creation.shud, PID: 19262
                  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: java.lang.SecurityException: ConnectivityService: Neither user 10244 nor current process has android.permission.ACCESS_NETWORK_STATE.
                      at android.os.Parcel.readException(Parcel.java:1553)
                      at android.os.Parcel.readException(Parcel.java:1505)
                      at android.net.IConnectivityManager$Stub$Proxy.getActiveNetworkInfo(IConnectivityManager.java:964)at android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:622)at com.amazonaws.mobileconnectors.cognito.DefaultDataset.isNetworkAvailable(DefaultDataset.java:580)at com.amazonaws.mobileconnectors.cognito.DefaultDataset.synchronize(DefaultDataset.java:128)at com.creation.shud.Manager.getCredentials(Manager.java:41)at com.creation.shud.EnterUser$UpdateTable.doInBackground(EnterUser.java:44)at com.creation.shud.EnterUser$UpdateTable.doInBackground(EnterUser.java:38)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)  I/Process: Sending signal. PID: 19262 SIG: 9 Application terminated.

这是我的代码:

EnterUser.java

public class EnterUser extends AppCompatActivity {
  EditText editText_name, editText_lastname;
  Button button_submit;
  String name, lastname;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enter_user);
    editText_name = (EditText) findViewById(R.id.et_name);
    editText_lastname = (EditText) findViewById(R.id.et_lastname);
    button_submit = (Button) findViewById(R.id.btn_submit);
    name = editText_name.getText().toString();
    lastname = editText_lastname.getText().toString();
    button_submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new UpdateTable().execute();
        }
    });
  }

  private class UpdateTable extends AsyncTask<Void, Integer, Integer> {

    @Override
    protected Integer doInBackground(Void... voids) {
        try {
            Manager manager = new Manager();
            CognitoCredentialsProvider credentialsProvider = manager.getCredentials(EnterUser.this);
            MapperClass mapperClass = new MapperClass();
            mapperClass.setName(name);
            mapperClass.setLastname(lastname);
            if (credentialsProvider != null && mapperClass != null) {
                DynamoDBMapper dynamoDBMapper = manager.initDynamoClient((CognitoCachingCredentialsProvider) credentialsProvider);
                dynamoDBMapper.save(mapperClass);
            } else {
                return 2;
            }
            return 1;

        } catch (AmazonClientException ex) {
            ex.printStackTrace();
            return 9999;
        }
    }

    @Override
    protected void onPostExecute(Integer integer) {
        super.onPostExecute(integer);
        if(integer==1){
            Toast.makeText(EnterUser.this, "Entered successfully :) ", Toast.LENGTH_SHORT).show();
        }else if(integer==2){
            Toast.makeText(EnterUser.this, "TRY AGAIN!!!.YOU are going to do it", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(EnterUser.this, "**Exception Caught**", Toast.LENGTH_SHORT).show();
        }
    }
  }
}

这是为了获取用户输入并保存在DynamoDB中

这里是MapperClass.java

@DynamoDBTable(tableName = "shtable")
public class MapperClass {
  String name;
  String lastname;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getLastname() {
    return lastname;
  }

  public void setLastname(String lastname) {
    this.lastname = lastname;
  }
}

这是Manager.java类

public class Manager {
  CognitoCredentialsProvider credentialsProvider =null;
  CognitoSyncManager syncManager = null;

  public  static AmazonDynamoDBClient dynamoDBClient =null;
  public  static DynamoDBMapper dynamoDBMapper = null;


  public CognitoCredentialsProvider getCredentials(Context context){
    // Initialize the Amazon Cognito credentials provider
    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
            context,
            "us-west-2:######-$$$$-!!!!-8795-!!f#####9###", // Identity     Pool ID
            Regions.US_WEST_2 // Region
    );
    syncManager = new CognitoSyncManager(context,Regions.US_WEST_2,credentialsProvider);
    Dataset dataset = syncManager.openOrCreateDataset("Mydataset");
    dataset.put("mykey","myvalue");
    dataset.synchronize(new DefaultSyncCallback());
    return credentialsProvider;
  }
}

public DynamoDBMapper initDynamoClient(CognitoCachingCredentialsProvider credentialsProvider){
  if(dynamoDBClient==null){
    dynamoDBClient = new AmazonDynamoDBClient(credentialsProvider);
        dynamoDBClient.setRegion(com.amazonaws.regions.Region.getRegion(Regions.US_WEST_2));
    dynamoDBMapper =new DynamoDBMapper(dynamoDBClient);
  }

  return  dynamoDBMapper;
}

1 个答案:

答案 0 :(得分:0)

“引起:java.lang.SecurityException:ConnectivityService:用户10244和当前进程都没有   android.permission.ACCESS_NETWORK_STATE。“是关键问题。

您的应用尚未声明ACCESS_NETWORK_STATE权限(结帐https://developer.android.com/guide/topics/manifest/uses-permission-element.html

作为旁注,我不确定你为什么要在getCredentials方法中初始化CognitoSync管理器......但你可能想把它分开。