它给了我AWS AWS的sns错误

时间:2018-10-12 00:21:53

标签: amazon-web-services amazon-sns

他尝试按照亚马逊的示例从android发送短信,但执行时却给我错误

我的代码:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AWSCredentials awsCredentials = new BasicAWSCredentials("accessKey", "secretKey");
    AmazonSNSClient snsClient = new AmazonSNSClient(awsCredentials);
    String message = "My SMS message";
    String phoneNumber = "+84933222053";
    Map<String, MessageAttributeValue> smsAttributes =
            new HashMap<String, MessageAttributeValue>();
    //<set SMS attributes>
    sendSMSMessage(snsClient, message, phoneNumber, smsAttributes);
}


public static void sendSMSMessage(AmazonSNSClient snsClient, String message,
                                  String phoneNumber, Map<String, MessageAttributeValue> smsAttributes) {
    PublishResult result = snsClient.publish(new PublishRequest()
            .withMessage(message)
            .withPhoneNumber(phoneNumber)
            .withMessageAttributes(smsAttributes));
    System.out.println(result); // Prints the message ID.
}}

我的错误

10-11 19:01:24.509 15584-15584/com.androfast.server.appmensajev2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androfast.server.appmensajev2, PID: 15584
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androfast.server.appmensajev2/com.androfast.server.appmensajev2.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
at android.app.ActivityThread.access$900(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5585)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                                                    Caused by: android.os.NetworkOnMainThreadException

请帮助我,我是亚马逊AWS的新手

0 个答案:

没有答案