AWSIoT Android SDK无法生成新证书+;钥匙

AWSIoT Android SDK无法生成新证书+;钥匙,android,amazon-web-services,aws-iot,Android,Amazon Web Services,Aws Iot,我对Android编程相当陌生,所以非常感谢您的指导。 我正在尝试生成连接到AWS IoT服务器的密钥+证书。它无法说明亚马逊域中的某个URL没有域名->地址解析。 Android日志消息是: I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.013_msm8974_LA.BF.1.1.1_RB1__re

我对Android编程相当陌生,所以非常感谢您的指导。 我正在尝试生成连接到AWS IoT服务器的密钥+证书。它无法说明亚马逊域中的某个URL没有域名->地址解析。 Android日志消息是:

I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.013_msm8974_LA.BF.1.1.1_RB1__release_AU ()
              OpenGL ES Shader Compiler Version: E031.25.03.00
              Build Date: 01/21/15 Wed
              Local Branch: mybranch7061829
              Remote Branch: quic/LA.BF.1.1.1_rb1.7
              Local Patches: NONE
              Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.013 +  NOTHING
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0
D/Atlas: Validating map...
D/libc-netbsd: [getaddrinfo]: hostname=xxxxx; servname=(null); cache_mode=(null), netid=0; mark=0
D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=xxxxx; ai_flags=4; ai_family=0
D/libc-netbsd: [getaddrinfo]: hostname=xxxxx; servname=(null); cache_mode=(null), netid=0; mark=0
D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=xxxxx; ai_flags=1024; ai_family=0
D/libc-netbsd: [getaddrinfo]: hostname=xxxxx; servname=(null); cache_mode=(null), netid=0; mark=0
D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=xxxxx; ai_flags=4; ai_family=0
I/art: Thread[1,tid=14654,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75c35b48,"main"] recursive attempt to load library "/system/lib/libhook_jni.so"
E/MediaProfilesEx-JNI: register_com_lge_media_MediaProfilesEx
E/MediaRecorderEx-JNI: register_com_lge_media_MediaRecorderEx
D/AudioSystemEx: register_com_lge_media_LGAudioSystem
E/SurfaceControlEx: register_com_lge_view_SurfaceControlEx
I/art: Thread[1,tid=14654,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75c35b48,"main"] recursive attempt to load library "/system/lib/libhook_jni.so"
D/LGMtpDatabaseJNI: register_android_mtp_LGMtpDatabase
I/art: Thread[1,tid=14654,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75c35b48,"main"] recursive attempt to load library "/system/lib/libhook_jni.so"
D/LGMtpServerJNI: register_android_mtp_LGMtpServer
I/art: Thread[1,tid=14654,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75c35b48,"main"] recursive attempt to load library "/system/lib/libhook_jni.so"
E/MediaPlayerEx-jni: register_com_lge_view_MediaPlayerEx
I/art: Thread[1,tid=14654,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75c35b48,"main"] recursive attempt to load library "/system/lib/libhook_jni.so"

       [ 12-29 22:00:44.433 14654:14654 D/         ]
       register_com_lge_emoji_EmojiUtil
D/libc-netbsd: [getaddrinfo]: hostname=xxxxx; servname=(null); cache_mode=(null), netid=0; mark=0
D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=xxxxx; ai_flags=4; ai_family=0
D/libc-netbsd: [getaddrinfo]: hostname=xxxxx; servname=(null); cache_mode=(null), netid=0; mark=0
D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=xxxxx; ai_flags=4; ai_family=0
W/System.err: com.amazonaws.AmazonClientException: Unable to execute HTTP request: Unable to resolve host "execute-api.eu-central-1.amazonaws.com": No address associated with hostname
W/System.err:     at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:424)
W/System.err:     at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:199)
W/System.err:     at com.amazonaws.services.iot.AWSIotClient.invoke(AWSIotClient.java:3713)
W/System.err:     at com.amazonaws.services.iot.AWSIotClient.createKeysAndCertificate(AWSIotClient.java:754)
W/System.err:     at com.home.tukai.awsiottryout01.MainActivity$3.run(MainActivity.java:188)
W/System.err:     at java.lang.Thread.run(Thread.java:818)
W/System.err: Caused by: java.net.UnknownHostException: Unable to resolve host "execute-api.eu-central-1.amazonaws.com": No address associated with hostname
W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:435)
W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:255)
W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:218)
W/System.err:     at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
W/System.err:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
W/System.err:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:303)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:242)
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:397)
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:229)
W/System.err:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
W/System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
W/System.err:     at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:156)
W/System.err:     at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:70)
W/System.err:     at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:356)
W/System.err:   ... 5 more
E/AWS: Exception occurred when generating new private key and certificate.
W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
I/Adreno EGL::EGL 1.4高通公司构建:AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.013_msm8974_LA.BF.1.1.1_RB1_发布版_()
OpenGL ES着色器编译器版本:E031.25.03.00
建造日期:2015年1月21日星期三
本地分行:mybranch7061829
远程分支机构:quic/LA.BF.1.1_rb1.7
本地修补程序:无
重构分支:AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.013+无
I/OpenGLRenderer:初始化EGL,版本1.4
D/OpenGLRenderer:启用调试模式0
地图集:正在验证地图。。。
D/libc netbsd:[getaddrinfo]:主机名=xxxxx;servname=(null);cache_mode=(null),netid=0;马克=0
D/libc netbsd:[getaddrinfo]:ai_addrlen=0;ai_canonname=xxxxx;爱_旗=4;艾尤族=0
D/libc netbsd:[getaddrinfo]:主机名=xxxxx;servname=(null);cache_mode=(null),netid=0;马克=0
D/libc netbsd:[getaddrinfo]:ai_addrlen=0;ai_canonname=xxxxx;ai_标志=1024;艾尤族=0
D/libc netbsd:[getaddrinfo]:主机名=xxxxx;servname=(null);cache_mode=(null),netid=0;马克=0
D/libc netbsd:[getaddrinfo]:ai_addrlen=0;ai_canonname=xxxxx;爱_旗=4;艾尤族=0
I/art:Thread[1,tid=14654,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75c35b48,“main”]递归尝试加载库“/system/lib/libhook_jni.so”
E/MediaProfilesEx JNI:注册\u com\u lge\u media\u MediaProfilesEx
E/MediaRecorderEx JNI:注册\u com\u lge\u media\u MediaRecorderEx
D/AudioSystemEx:注册\u com\u lge\u媒体\u LGAudioSystem
E/SurfaceControlEx:注册\u com\u lge\u视图\u SURFACONTROLEX
I/art:Thread[1,tid=14654,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75c35b48,“main”]递归尝试加载库“/system/lib/libhook_jni.so”
D/LGMtpDatabaseJNI:注册\u android\u mtp\u LGMtpDatabase
I/art:Thread[1,tid=14654,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75c35b48,“main”]递归尝试加载库“/system/lib/libhook_jni.so”
D/LGMtpServerJNI:register_android_mtp_LGMtpServer
I/art:Thread[1,tid=14654,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75c35b48,“main”]递归尝试加载库“/system/lib/libhook_jni.so”
E/MediaPlayerEx jni:注册\u com\u lge\u视图\u MediaPlayerEx
I/art:Thread[1,tid=14654,WaitingForJniOnLoad,Thread*=0xb4827800,peer=0x75c35b48,“main”]递归尝试加载库“/system/lib/libhook_jni.so”
[12-29 22:00:44.433 14654:14654 D/]
注册\u com\u lge\u表情符号\u EmojiUtil
D/libc netbsd:[getaddrinfo]:主机名=xxxxx;servname=(null);cache_mode=(null),netid=0;马克=0
D/libc netbsd:[getaddrinfo]:ai_addrlen=0;ai_canonname=xxxxx;爱_旗=4;艾尤族=0
D/libc netbsd:[getaddrinfo]:主机名=xxxxx;servname=(null);cache_mode=(null),netid=0;马克=0
D/libc netbsd:[getaddrinfo]:ai_addrlen=0;ai_canonname=xxxxx;爱_旗=4;艾尤族=0
W/System.err:com.amazonaws.AmazonClientException:无法执行HTTP请求:无法解析主机“execute api.eu-central-1.amazonaws.com”:没有与主机名关联的地址
W/System.err:com.amazonaws.http.AmazonHttpClient.executehelp(AmazonHttpClient.java:424)
W/System.err:com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:199)
W/System.err:com.amazonaws.services.iot.AWSIotClient.invoke(AWSIotClient.java:3713)
W/System.err:com.amazonaws.services.iot.AWSIotClient.createKeysAndCertificate(AWSIotClient.java:754)
W/System.err:com.home.tukai.awsiottryout01.MainActivity$3.run(MainActivity.java:188)
W/System.err:at java.lang.Thread.run(Thread.java:818)
W/System.err:原因:java.net.UnknownHostException:无法解析主机“execute api.eu-central-1.amazonaws.com”:没有与主机名关联的地址
W/System.err:at java.net.InetAddress.lookupHostByName(InetAddress.java:435)
W/System.err:at java.net.InetAddress.getAllByNameImpl(InetAddress.java:255)
W/System.err:at java.net.InetAddress.getAllByName(InetAddress.java:218)
W/System.err:com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
W/System.err:com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
W/System.err:com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
W/System.err:com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:303)
W/System.err:com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:242)
W/System.err:com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:397)
W/System.err:com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
W/System.err:com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:229)
W/System.err:com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
W/System.err:com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
W/System.err:com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:156)
W/System.err:com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:70)
W/System.err:com.amazonaws.http.AmazonHttpClient.executehelp(AmazonHttpClient.java:356)
W/System.err:。。。还有5个
E/AWS:生成新私钥和证书时发生异常。
W/IIInputConnectionWrapper:在非活动InputConnection上显示状态图标
现在,有趣的是,如果我将密钥库加载到手机中应用程序包的位置,那么它就可以正常工作。然后不检查(KeyStore==null)。 这里的重点是crea
private static final String CUSTOMER_SPECIFIC_ENDPOINT = "a228q6docijn1f.iot.eu-central-1.amazonaws.com";
// Cognito pool ID. For this app, pool needs to be unauthenticated pool with
// AWS IoT permissions.
private static final String COGNITO_POOL_ID = "eu-central-1:36b201e2-d55e-4601-9430-031fc7990291";
// Name of the AWS IoT policy to attach to a newly created certificate
private static final String AWS_IOT_POLICY_NAME = "all-iot";

// Region of AWS IoT
private static final Regions MY_REGION = Regions.EU_CENTRAL_1;
// Filename of KeyStore file on the filesystem
private static final String KEYSTORE_NAME = "KeyStoreAlfred01.bks";
// Password for the private key in the KeyStore
private static final String KEYSTORE_PASSWORD = "<THIS I FILLED IN CORRECTLY>";
// Certificate and key aliases in the KeyStore
private static final String CERTIFICATE_ID = "keystorealfred01";


AWSIotClient mIotAndroidClient;
AWSIotMqttManager mqttManager;
String clientId;
String keystorePath;
String keystoreName;
String keystorePassword;

KeyStore clientKeyStore = null;
String certificateId;

CognitoCachingCredentialsProvider credentialsProvider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    btnGenNumber = (Button) findViewById(R.id.btnGenNumber);
    btnConnect = (Button) findViewById(R.id.btnConnect);
    btnYoutubeSearch = (Button) findViewById(R.id.btnYoutubeSearch);

    btnGenNumber.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Random r = new Random();
            int num = r.nextInt(51);

            try {
                mqttManager.publishString(Integer.toString(num), "test", AWSIotMqttQos.QOS0);
                Toast.makeText(getApplicationContext(), "Published number " + Integer.toString(num), Toast.LENGTH_LONG).show();

                Log.i(LOG_TAG, "Publish done");
            } catch (Exception e) {
                Log.e(LOG_TAG, "Publish error.", e);
            }

            Log.i(LOG_TAG, "Button generated a new number" + num);
        }
    });

    btnYoutubeSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_SEARCH);
            intent.setPackage("com.google.android.youtube");
            intent.putExtra("query", "Android");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });
    // Initialize the AWS Cognito credentials provider
    credentialsProvider = new CognitoCachingCredentialsProvider(
            getApplicationContext(), // context
            COGNITO_POOL_ID, // Identity Pool ID
            MY_REGION // Region
    );
    Log.i(LOG_TAG, "Credentials provider output " + credentialsProvider.toString());
    Region region = Region.getRegion(MY_REGION);
    mIotAndroidClient = new AWSIotClient(credentialsProvider);
    mIotAndroidClient.setRegion(region);

    //keystorePath = "/mnt/sdcard/awsIOTtryout01";
    keystorePath = getFilesDir().getPath();
    keystoreName = KEYSTORE_NAME;
    keystorePassword = KEYSTORE_PASSWORD;
    certificateId = CERTIFICATE_ID;

    Log.i(LOG_TAG, "Begin certificate load");

    // To load cert/key from keystore on filesystem
    try {
        if (AWSIotKeystoreHelper.isKeystorePresent(keystorePath, keystoreName)) {
            if (AWSIotKeystoreHelper.keystoreContainsAlias(certificateId, keystorePath,
                    keystoreName, keystorePassword)) {
                Log.i(LOG_TAG, "Certificate " + certificateId
                        + " found in keystore - using for MQTT.");
                // load keystore from file into memory to pass on connection
                clientKeyStore = AWSIotKeystoreHelper.getIotKeystore(certificateId,
                        keystorePath, keystoreName, keystorePassword);
                Log.i(LOG_TAG, "Certificate loading complete");
                //btnConnect.setEnabled(true);
            } else {
                Log.i(LOG_TAG, "Key/cert " + certificateId + " not found in keystore.");
            }
        } else {
            Log.i(LOG_TAG, "Keystore " + keystorePath + "/" + keystoreName + " not found.");
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "An error occurred retrieving cert/key from keystore.", e);
    }

    if (clientKeyStore == null) {
        Toast.makeText(getApplicationContext(),"Cert/key was not found in keystore - creating new key and certificate.",Toast.LENGTH_SHORT).show();
        Log.i(LOG_TAG, "Cert/key was not found in keystore - creating new key and certificate.");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // Create a new private key and certificate. This call
                    // creates both on the server and returns them to the
                    // device.
                    CreateKeysAndCertificateRequest createKeysAndCertificateRequest =
                            new CreateKeysAndCertificateRequest();
                    createKeysAndCertificateRequest.setSetAsActive(true);
                    Log.i(LOG_TAG,"keys + cert generation : "+createKeysAndCertificateRequest.toString());
                    final CreateKeysAndCertificateResult createKeysAndCertificateResult;
                    createKeysAndCertificateResult =mIotAndroidClient.createKeysAndCertificate(createKeysAndCertificateRequest);

                    //Log.i(LOG_TAG,"Cert ID: New certificate generation done.");
                    //Toast.makeText(getApplicationContext(),"New certificate created", Toast.LENGTH_SHORT).show();
                    // 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,
                            createKeysAndCertificateResult.getCertificatePem(),
                            createKeysAndCertificateResult.getKeyPair().getPrivateKey(),
                            keystorePath, keystoreName, keystorePassword);

                    // load keystore from file into memory to pass on connection

                    clientKeyStore = AWSIotKeystoreHelper.getIotKeystore(certificateId,
                            keystorePath, keystoreName, keystorePassword);
                    // Attach a policy to the newly created certificate
                    // This flow assumes the policy was already created in
                    // AWS IoT and we are now just attaching it to the
                    // certificate.

                    AttachPrincipalPolicyRequest policyAttachRequest =
                            new AttachPrincipalPolicyRequest();
                    policyAttachRequest.setPolicyName(AWS_IOT_POLICY_NAME);
                    policyAttachRequest.setPrincipal(createKeysAndCertificateResult.getCertificateArn());
                    mIotAndroidClient.attachPrincipalPolicy(policyAttachRequest);
                } catch (Exception e) {
                   // Toast.makeText(getApplicationContext(),"Exception occured when generating new key+cert",Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                    Log.e(LOG_TAG,"Exception occurred when generating new private key and certificate.",e);
                }
            }
        }).start();
    }
/CognitoCachingCredentia`enter code here`lsProvider: Loading credentials from SharedPreferences
I/AWS: Credentials provider output com.amazonaws.auth.CognitoCachingCredentialsProvider@2d7ae714
I/AmazonWebServiceClient: {execute-api, eu-central-1} was not found in region metadata, trying to construct an endpoint using the standard pattern for this region: 'execute-api.eu-central-1.amazonaws.com'.
I/AWS: Begin certificate load
I/AWS: Certificate keystorealfred01 found in keystore - using for MQTT.
I/AWS: Certificate loading complete
D/OpenGLRenderer: Render dirty regions requested: true
I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.013_msm8974_LA.BF.1.1.1_RB1__release_AU ()
              OpenGL ES Shader Compiler Version: E031.25.03.00
              Build Date: 01/21/15 Wed
              Local Branch: mybranch7061829
              Remote Branch: quic/LA.BF.1.1.1_rb1.7
              Local Patches: NONE
              Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.013 +  NOTHING
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0
D/Atlas: Validating map...
W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@25603903 time:661926148