这是我正在使用的连接到Amazon dynamo DB的源代码片段
CognitoCachingCredentialsProvider credentialsProvider =
new CognitoCachingCredentialsProvider(
this, // get the context for the current activity
"XXXXX027930", // your AWS Account id
"us-east-1:e7a5fXXc-3c9c-4644-b754-3604188c6d99",// your identity pool id
"arn:aws:iam::31XXX0027930:role/hmo_users",// an authenticated role ARN
"arn:aws:iam::31289XXX7930:role/hmo_users", // an unauthenticated role ARN
Regions.US_EAST_1 //Region
);
//Log.d("LogTag", "my ID is " + credentialsProvider.getIdentityId());
AmazonDynamoDB client = new AmazonDynamoDBClient(credentialsProvider);
List<String> l = client.listTables().getTableNames();
for(int i=0;i<l.size();i++)
{
System.out.println("Table "+i+"is :"+l.get(i));
}
我收到以下错误:
05-01 15:24:11.687: I/System.out(2858): The main activity has been started
05-01 15:24:11.798: I/art(2858): Background sticky concurrent mark sweep GC freed 3058(229KB) AllocSpace objects, 0(0B) LOS objects, 27% free, 826KB/1135KB, paused 93.590ms total 115.769ms
05-01 15:24:11.945: I/Choreographer(2858): Skipped 76 frames! The application may be doing too much work on its main thread.
05-01 15:24:12.065: D/gralloc_goldfish(2858): Emulator without GPU emulation detected.
05-01 15:24:12.222: I/art(2858): Background partial concurrent mark sweep GC freed 2482(150KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 1049KB/2MB, paused 6.325ms total 156.047ms
05-01 15:24:18.140: D/CognitoCachingCredentialsProvider(2858): Loading credentials from SharedPreferences
05-01 15:24:18.140: D/CognitoCachingCredentialsProvider(2858): No valid credentials found in SharedPreferences
05-01 15:24:18.165: D/CognitoCachingCredentialsProvider(2858): Loading credentials from SharedPreferences
05-01 15:24:18.165: D/CognitoCachingCredentialsProvider(2858): No valid credentials found in SharedPreferences
05-01 15:24:18.177: D/AndroidRuntime(2858): Shutting down VM
05-01 15:24:18.178: E/AndroidRuntime(2858): FATAL EXCEPTION: main
05-01 15:24:18.178: E/AndroidRuntime(2858): Process: com.example.hmo, PID: 2858
05-01 15:24:18.178: E/AndroidRuntime(2858): java.lang.IllegalStateException: Could not execute method of the activity
05-01 15:24:18.178: E/AndroidRuntime(2858): at android.view.View$1.onClick(View.java:4007)
05-01 15:24:18.178: E/AndroidRuntime(2858): at android.view.View.performClick(View.java:4756)
05-01 15:24:18.178: E/AndroidRuntime(2858): at android.view.View$PerformClick.run(View.java:19749)
05-01 15:24:18.178: E/AndroidRuntime(2858): at android.os.Handler.handleCallback(Handler.java:739)
05-01 15:24:18.178: E/AndroidRuntime(2858): at android.os.Handler.dispatchMessage(Handler.java:95)
05-01 15:24:18.178: E/AndroidRuntime(2858): at android.os.Looper.loop(Looper.java:135)
05-01 15:24:18.178: E/AndroidRuntime(2858): at android.app.ActivityThread.main(ActivityThread.java:5221)
05-01 15:24:18.178: E/AndroidRuntime(2858): at java.lang.reflect.Method.invoke(Native Method)
05-01 15:24:18.178: E/AndroidRuntime(2858): at java.lang.reflect.Method.invoke(Method.java:372)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-01 15:24:18.178: E/AndroidRuntime(2858): Caused by: java.lang.reflect.InvocationTargetException
05-01 15:24:18.178: E/AndroidRuntime(2858): at java.lang.reflect.Method.invoke(Native Method)
05-01 15:24:18.178: E/AndroidRuntime(2858): at java.lang.reflect.Method.invoke(Method.java:372)
05-01 15:24:18.178: E/AndroidRuntime(2858): at android.view.View$1.onClick(View.java:4002)
05-01 15:24:18.178: E/AndroidRuntime(2858): ... 10 more
05-01 15:24:18.178: E/AndroidRuntime(2858): Caused by: android.os.NetworkOnMainThreadException
05-01 15:24:18.178: E/AndroidRuntime(2858): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
05-01 15:24:18.178: E/AndroidRuntime(2858): at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
05-01 15:24:18.178: E/AndroidRuntime(2858): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
05-01 15:24:18.178: E/AndroidRuntime(2858): at java.net.InetAddress.getAllByName(InetAddress.java:215)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.http.UrlHttpClient.createConnection(UrlHttpClient.java:142)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:60)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:353)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:196)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.invoke(AmazonCognitoIdentityClient.java:455)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getId(AmazonCognitoIdentityClient.java:290)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.getIdentityId(AWSAbstractCognitoIdentityProvider.java:170)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.refresh(AWSAbstractCognitoIdentityProvider.java:312)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.auth.AWSBasicCognitoIdentityProvider.refresh(AWSBasicCognitoIdentityProvider.java:77)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:533)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.auth.CognitoCredentialsProvider.getCredentials(CognitoCredentialsProvider.java:366)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:440)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:76)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:2918)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.listTables(AmazonDynamoDBClient.java:1372)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.listTables(AmazonDynamoDBClient.java:1401)
05-01 15:24:18.178: E/AndroidRuntime(2858): at com.example.hmo.MainActivity.storeValues(MainActivity.java:98)
05-01 15:24:18.178: E/AndroidRuntime(2858): ... 13 more
05-01 15:24:22.725: I/Process(2858): Sending signal. PID: 2858 SIG: 9
有人可以帮助解决此错误。
提前致谢。
最佳答案
基本上,网络过程必须不在UI线程中完成。
您应该使用Thread或AsyncTask来包装网络或AWS通信或MainActivity.storeValues(MainActivity.java:98)
中发生的一切。