首页 > 解决方案 > 测试 aws-sdk-android-samples/AndroidPubSub 但连接被拒绝

问题描述

我试图从中进行测试aws-sdk-android-samples/AndroidPubSub/https://github.com/awslabs/aws-sdk-android-samples/tree/master/AndroidPubSub但是在单击连接后,总是会收到错误消息

30000 毫秒后 a2k94wsqkar4rm-ats.iot.us-west-2.amazonaws.com/52.13.183.162(端口 8883):isConnected 失败:ECONNREFUSED(连接被拒绝)

我在 AWS IoT 控制台上创建了我的设备证书并将其激活,并附加了如下所述的策略,

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:Connect",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "iot:Publish",
        "iot:Subscribe",
        "iot:Receive"
      ],
      "Resource": "*"
    }
  ]
}

我尝试了两种方法将证书和密钥安装到本地密钥库,并且它们都有效。

方法一,通过源码

AssetManager assetManager = getAssets();

// read cert and private key from pem file
InputStream input = assetManager.open("4ed2c76117-private.pem");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
String privateKeyPem = new String(buffer);
System.out.println(privateKeyPem);

input = assetManager.open("4ed2c76117-certificate.pem");
size = input.available();
byte[] buffer2 = new byte[size];
input.read(buffer2);
input.close();
String certPem = new String(buffer2);
System.out.println(certPem);

// store in keystore for use in MQTT client
// saved as alias "default" so a new certificate isn't
// generated each run of this application
AWSIotKeystoreHelper.saveCertificateAndPrivateKey(certificateId,
certPem,
privateKeyPem,
keystorePath, keystoreName, keystorePassword);

// load keystore from file into memory to pass on
// connection
clientKeyStore = AWSIotKeystoreHelper.getIotKeystore(certificateId,
keystorePath, keystoreName, keystorePassword);

方法2,通过终端命令,

openssl pkcs12 -export -out iot_keystore.p12 -inkey 4ed2c76117-private.pem -in 4ed2c76117-certificate.pem -name default

keytool -importkeystore -srckeystore iot_keystore.p12 -srcstoretype pkcs12 -destkeystore iot_keystore.bks -deststoretype bks --provider org.bouncycastle.jce.provider.BouncyCastleProvider -–providerpath bcprov-jdk15on-164.jar

adb push iot_keystore.bks /data/user/0/com.amazonaws.demo.androidpubsub/files/iot_keystore

任何人都可以帮助解决这个问题吗?

标签: androidaws-sdkmqttaws-iot

解决方案


对不起,我认为这是一个模拟器网络问题。正确连接网络后,这个问题就消失了。


推荐阅读