gpt4 book ai didi

java - 无法在 Android 中创建 Amazon Sqs,但可以在 Java 应用程序中创建它

转载 作者:行者123 更新时间:2023-12-01 12:57:31 26 4
gpt4 key购买 nike

我无法在 Android 中创建队列,但使用与在 java 应用程序中创建的代码相同的代码。我应该怎么办?

这是我的代码 fragment :

AmazonSQS sqs = new AmazonSQSClient( new BasicAWSCredentials( "access key", "secret key" ));
String Url = sqs.createQueue(new CreateQueueRequest(email)).getQueueUrl();

这是我的堆栈跟踪:

05-20 08:28:24.187: W/dalvikvm(1452): threadid=1: thread exiting with uncaught exception (group=0xb1a85ba8)
05-20 08:28:24.247: E/AndroidRuntime(1452): FATAL EXCEPTION: main
05-20 08:28:24.247: E/AndroidRuntime(1452): Process: com.cloud.sensor, PID: 1452
05-20 08:28:24.247: E/AndroidRuntime(1452): android.os.NetworkOnMainThreadException
05-20 08:28:24.247: E/AndroidRuntime(1452): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
05-20 08:28:24.247: E/AndroidRuntime(1452): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-20 08:28:24.247: E/AndroidRuntime(1452): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-20 08:28:24.247: E/AndroidRuntime(1452): at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:278)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:377)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:228)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:1004)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.amazonaws.services.sqs.AmazonSQSClient.createQueue(AmazonSQSClient.java:845)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.cloud.sensor.Register.register(Register.java:80)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.cloud.sensor.Register$1.onClick(Register.java:48)
05-20 08:28:24.247: E/AndroidRuntime(1452): at android.view.View.performClick(View.java:4438)
05-20 08:28:24.247: E/AndroidRuntime(1452): at android.view.View$PerformClick.run(View.java:18422)
05-20 08:28:24.247: E/AndroidRuntime(1452): at android.os.Handler.handleCallback(Handler.java:733)
05-20 08:28:24.247: E/AndroidRuntime(1452): at android.os.Handler.dispatchMessage(Handler.java:95)
05-20 08:28:24.247: E/AndroidRuntime(1452): at android.os.Looper.loop(Looper.java:136)
05-20 08:28:24.247: E/AndroidRuntime(1452): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-20 08:28:24.247: E/AndroidRuntime(1452): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 08:28:24.247: E/AndroidRuntime(1452): at java.lang.reflect.Method.invoke(Method.java:515)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-20 08:28:24.247: E/AndroidRuntime(1452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-20 08:28:24.247: E/AndroidRuntime(1452): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您正在主线程上调用代码 fragment 。您需要在后台线程中运行它。实现此目的的一种方法是使用 AsyncTask .

关于java - 无法在 Android 中创建 Amazon Sqs,但可以在 Java 应用程序中创建它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23760872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com