ManveenT ManveenT - 28 days ago 8
Java Question

I'm trying to store user's name and last name in DynamoDB.Im getting following errors

In spite of following amazon docs and various tutorials on internet, still I'm unable to figure out these errors:

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.


Here is my Code:

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


This is to get the user input and save in DynamoDB

while here is 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;
}
}


and here is the Manager.java class

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

Answer

" Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10244 nor current process has android.permission.ACCESS_NETWORK_STATE." Is the key issue.

Your app hasn't declared the ACCESS_NETWORK_STATE permission (checkout https://developer.android.com/guide/topics/manifest/uses-permission-element.html)

As a side note, I'm not sure why you are initializing the CognitoSync manager inside the getCredentials method... but you probably want to separate it out.