我正在開發一個用戶嘗試上傳文件到AWS S3的Android項目。開發人員身份驗證使用AWS Cognito完成。因此,在此過程是在用戶嘗試將文件上傳到S3之前,用戶必須使用AWS Cogntio獲取憑證。之後用戶上傳文件。這個過程發生在用戶選擇文件的背景,然後單擊確定。如果互聯網可用,一切都會好起來。否則,應用程序崩潰。 這是代碼的一部分。當沒有互聯網連接時,應用程序崩潰
這裏Auth類擴展了AWSAbstractCognitoDeveloperIdentityProvider。
Auth developerProvider = new Auth(
null,
"ap-northeast-1:a871fa5f-2-480d-baa6-b4ed31437244",
Regions.AP_NORTHEAST_1);
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
this.ctx.getApplicationContext(),
developerProvider,
Regions.AP_NORTHEAST_1);
HashMap<String, String> loginsMap = new HashMap<String, String>();
loginsMap.put("login.cool.app", "7386872");
credentialsProvider.setLogins(loginsMap);
credentialsProvider.refresh();
應用在該行
credentialsProvider.refresh();
錯誤它顯示崩潰:
I/AmazonHttpClient: Unable to execute HTTP request: Unable to resolve host "cognito-identity.ap-northeast-1.amazonaws.com": No address associated with hostname
java.net.UnknownHostException: Unable to resolve host "cognito-identity.ap-northeast-1.amazonaws.com": No address associated with hostname
at java.net.InetAddress.lookupHostByName(InetAddress.java:427)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:217)
at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
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.getCredentialsForIdentity(AmazonCognitoIdentityClient.java:406)
at com.amazonaws.auth.CognitoCredentialsProvider.populateCredentialsWithCognito(CognitoCredentialsProvider.java:627)
at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:553)
at com.amazonaws.auth.CognitoCredentialsProvider.refresh(CognitoCredentialsProvider.java:503)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.refresh(CognitoCachingCredentialsProvider.java:462)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.getIdentityId(CognitoCachingCredentialsProvider.java:413)
at com.amazonaws.auth.CognitoCredentialsProvider.populateCredentialsWithCognito(CognitoCredentialsProvider.java:620)
at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:553)
at com.amazonaws.auth.CognitoCredentialsProvider.refresh(CognitoCredentialsProvider.java:503)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.refresh(CognitoCachingCredentialsProvider.java:462)
at com.example.sandesh.filer.UpDown.upload.doInBackground(upload.java:89)
at com.example.sandesh.filer.UpDown.upload.doInBackground(upload.java:27)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
反正有沒有嘗試抓住它?或任何解決方案,以避免崩潰?
謝謝。
http://developer.android.com/reference/android/net/NetworkInfo.html#isConnected%28%29 – F43nd1r
你爲什麼不先檢查互聯網連接? –
棧跟蹤不是來自崩潰,而是INFO級別的日誌消息。系統使用ERROR日誌級別記錄崩潰。 – laalto