0

我正在尝试将图像文件上传到 aws 存储桶,但有时我在设备上遇到此错误,

I/AmazonHttpClient: Unable to execute HTTP request: Unable to resolve host "cognito-identity.us-east-1.amazonaws.com": No address associated with hostname
                                                                  java.net.UnknownHostException: Unable to resolve host "cognito-identity.us-east-1.amazonaws.com": No address associated with hostname
                                                                      at java.net.InetAddress.lookupHostByName(InetAddress.java:400)
                                                                      at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
                                                                      at java.net.InetAddress.getAllByName(InetAddress.java:220)
                                                                      at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
                                                                      at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
                                                                      at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
                                                                      at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
                                                                      at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
                                                                      at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
                                                                      at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
                                                                      at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:441)
                                                                      at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
                                                                      at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
                                                                      at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
                                                                      at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
                                                                      at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:280)
                                                                      at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:128)
                                                                      at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:65)
                                                                      at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:353)
                                                                      at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:196)
                                                                      at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.invoke(AmazonCognitoIdentityClient.java:533)
                                                                      at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getId(AmazonCognitoIdentityClient.java:344)
                                                                      at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.getIdentityId(AWSAbstractCognitoIdentityProvider.java:170)
                                                                      at com.amazonaws.auth.AWSEnhancedCognitoIdentityProvider.refresh(AWSEnhancedCognitoIdentityProvider.java:76)
                                                                      at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:537)
                                                                      at com.amazonaws.auth.CognitoCredentialsProvider.getCredentials(CognitoCredentialsProvider.java:370)
                                                                      at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:440)
                                                                      at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:76)
                                                                      at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4198)
                                                                      at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1618)
                                                                      at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.uploadSinglePartAndWaitForCompletion(UploadTask.java:174)
                                                                      at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.call(UploadTask.java:74)
                                                                      at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.call(UploadTask.java:40)
                                                                      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
                                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:137)
                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
                                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
                                                                      at java.lang.Thread.run(Thread.java:856)

但一段时间后,它显示像,

D/CognitoCachingCredentialsProvider: Identity id is changed
12-12 11:27:25.771 11239-14564/D/CognitoCachingCredentialsProvider: Saving identity id to SharedPreferences
12-12 11:27:25.771 11239-11246/com.amlooking4 I/dalvikvm: Jit: resizing JitTable from 4096 to 8192
12-12 11:27:26.161 11239-14564/D/CognitoCachingCredentialsProvider: Saving credentials to SharedPreferences

编辑:

现在我也得到了这个,

    I/AmazonHttpClient: Unable to execute HTTP request: SSL handshake timed out
                                                                      java.net.SocketTimeoutException: SSL handshake timed out

 Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x1e5dff8: Failure in SSL library, usually a protocol error
                                                                  error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:658 0x401d6cf5:0x00000000)
                                                                      at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
                                                                      at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
                                                                        ... 23 more

真的因为这种不当行为而感到沮丧,代码是相同的,但有时它执行得很好,有时却没有,并显示上述错误。

我是aws的新手,我知道它将认知ID保存到preferneces中。

4

1 回答 1

2

将此代码添加到 Utils.Java

    private static CognitoCachingCredentialsProvider getCredProvider(Context context) {
    if (sCredProvider == null) {
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeout(30000);
        clientConfiguration.setSocketTimeout(30000);
        sCredProvider = new CognitoCachingCredentialsProvider(
                context.getApplicationContext(),
                Constants.COGNITO_POOL_ID,
                Regions.fromName(Constants.COGNITO_POOL_REGION),clientConfiguration);
    }
    return sCredProvider;
};    

和 amazonS3Client 假设看起来像这样:

     public static AmazonS3Client getS3Client(Context context) {
    if (sS3Client == null) {
        sS3Client = new AmazonS3Client(getCredProvider(context.getApplicationContext()));
        sS3Client.setRegion(Region.getRegion(Regions.fromName(Constants.BUCKET_REGION)));

    }
    return sS3Client;
};
于 2017-05-09T06:58:04.553 回答