我正在尝试在Samsung Galaxy 4S(Android 4.4)上测试AWS SimpleDB连接。这是我第一次为Android平台编写应用程序,但我还不太熟悉这里的工作方式。下面是我绑定到UI上的一个按钮。该功能的主要目的应该是连接到AWS,然后为数据创建域。
public void select_no1(View view) {
AWSCredentials credentials = new BasicAWSCredentials("MY_KEY_ID","MY_SECRET_ACCESS_KEY");
AmazonSimpleDBClient sdbClient = new AmazonSimpleDBClient(credentials);
sdbClient.setEndpoint("us-east-1");
sdbClient.listDomains();
CreateDomainRequest cdr = new CreateDomainRequest("test_from_and");
sdbClient.createDomain(cdr);
}
一开始它工作得很好,但是一旦我添加了这两行,应用就会立即崩溃:
CreateDomainRequest cdr = new CreateDomainRequest("test_from_and");
sdbClient.createDomain(cdr);
实际上,这样做也会使应用程序崩溃。
sdbClient.listDomains();
以下是我从LogCat得到的错误。我不确定这里有什么不对。 :(
05-08 18:45:54.858: E/AndroidRuntime(17016): FATAL EXCEPTION: main
05-08 18:45:54.858: E/AndroidRuntime(17016): java.lang.IllegalStateException: Could not execute method of the activity
05-08 18:45:54.858: E/AndroidRuntime(17016): at android.view.View$1.onClick(View.java:3614)
05-08 18:45:54.858: E/AndroidRuntime(17016): at android.view.View.performClick(View.java:4219)
05-08 18:45:54.858: E/AndroidRuntime(17016): at android.view.View$PerformClick.run(View.java:17538)
05-08 18:45:54.858: E/AndroidRuntime(17016): at android.os.Handler.handleCallback(Handler.java:800)
05-08 18:45:54.858: E/AndroidRuntime(17016): at android.os.Handler.dispatchMessage(Handler.java:100)
05-08 18:45:54.858: E/AndroidRuntime(17016): at android.os.Looper.loop(Looper.java:194)
05-08 18:45:54.858: E/AndroidRuntime(17016): at android.app.ActivityThread.main(ActivityThread.java:5371)
05-08 18:45:54.858: E/AndroidRuntime(17016): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 18:45:54.858: E/AndroidRuntime(17016): at java.lang.reflect.Method.invoke(Method.java:525)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-08 18:45:54.858: E/AndroidRuntime(17016): at dalvik.system.NativeStart.main(Native Method)
05-08 18:45:54.858: E/AndroidRuntime(17016): Caused by: java.lang.reflect.InvocationTargetException
05-08 18:45:54.858: E/AndroidRuntime(17016): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 18:45:54.858: E/AndroidRuntime(17016): at java.lang.reflect.Method.invoke(Method.java:525)
05-08 18:45:54.858: E/AndroidRuntime(17016): at android.view.View$1.onClick(View.java:3609)
05-08 18:45:54.858: E/AndroidRuntime(17016): ... 11 more
05-08 18:45:54.858: E/AndroidRuntime(17016): Caused by: android.os.NetworkOnMainThreadException
05-08 18:45:54.858: E/AndroidRuntime(17016): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
05-08 18:45:54.858: E/AndroidRuntime(17016): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-08 18:45:54.858: E/AndroidRuntime(17016): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-08 18:45:54.858: E/AndroidRuntime(17016): at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:278)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.http.AmazonHttpClient.executeHelper(Unknown Source)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.http.AmazonHttpClient.execute(Unknown Source)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.services.simpledb.AmazonSimpleDBClient.invoke(Unknown Source)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.services.simpledb.AmazonSimpleDBClient.listDomains(Unknown Source)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.amazonaws.services.simpledb.AmazonSimpleDBClient.listDomains(Unknown Source)
05-08 18:45:54.858: E/AndroidRuntime(17016): at com.example.exitpoll.MainActivity.select_no1(MainActivity.java:85)
05-08 18:45:54.858: E/AndroidRuntime(17016): ... 14 more
05-08 18:45:56.653: I/Process(17016): Sending signal. PID: 17016 SIG: 9
答案 0 :(得分:1)
您正在UI线程中建立网络连接。使它成为单独的线程
尝试这个简单的解决方案。你需要根据你的功能来处理。
public void select_no1(View view) {
new Thread(new Runnable() {
@Override
public void run() {
AWSCredentials credentials = new BasicAWSCredentials("MY_KEY_ID","MY_SECRET_ACCESS_KEY");
AmazonSimpleDBClient sdbClient = new AmazonSimpleDBClient(credentials);
sdbClient.setEndpoint("us-east-1");
sdbClient.listDomains();
CreateDomainRequest cdr = new CreateDomainRequest("test_from_and");
sdbClient.createDomain(cdr);
}
}).start();
}