刷新时 CognitoCredentialsProvider 抛出 runtimeException

Posted

技术标签:

【中文标题】刷新时 CognitoCredentialsProvider 抛出 runtimeException【英文标题】:CognitoCredentialsProvider on refresh throws runtimeException 【发布时间】:2014-09-27 17:49:38 【问题描述】:

我正在尝试从我的 android 应用程序将文本文件上传到 Amazon s3。我正在使用 Amazon AWS 提供的以下代码 sn-p。

private static CognitoCredentialsProvider sCredProvider;

public static CognitoCredentialsProvider getCredProvider(Context context) 
    if(sCredProvider == null) 
        Log.i("Util","reached utility");
        Log.i("Util","Context"+context);
        sCredProvider = new CognitoCredentialsProvider(
                context,
                Constants.AWS_ACCOUNT_ID,
                Constants.COGNITO_POOL_ID,
                Constants.COGNITO_ROLE_UNAUTH,
                null);
        Log.i("Util","returning scredprovider"+sCredProvider);
        sCredProvider.refresh();
    
    Log.i("Util","returning scredprovider"+sCredProvider);
    return sCredProvider;

当代码通过sCredProvider.refresh();它会引发跟随错误。

08-05 02:21:24.908: E/AndroidRuntime(25712): FATAL EXCEPTION: main
08-05 02:21:24.908: E/AndroidRuntime(25712): java.lang.RuntimeException: Unable to   create service network.NetworkService: android.os.NetworkOnMainThreadException
08-05 02:21:24.908: E/AndroidRuntime(25712):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2667)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at android.app.ActivityThread.access$1600(ActivityThread.java:153)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at android.os.Looper.loop(Looper.java:137)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at android.app.ActivityThread.main(ActivityThread.java:5227)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at java.lang.reflect.Method.invokeNative(Native Method)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at java.lang.reflect.Method.invoke(Method.java:511)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at dalvik.system.NativeStart.main(Native Method)
08-05 02:21:24.908: E/AndroidRuntime(25712): Caused by: android.os.NetworkOnMainThreadException
08-05 02:21:24.908: E/AndroidRuntime(25712):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:278)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.http.AmazonHttpClient.executeHelper(Unknown Source)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.http.AmazonHttpClient.execute(Unknown Source)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityServiceClient.invoke(Unknown Source)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityServiceClient.getOpenIdToken(Unknown Source)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.auth.CognitoCredentialsProvider.startSession(Unknown Source)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.amazonaws.auth.CognitoCredentialsProvider.refresh(Unknown Source)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at com.chan.censioevlog.Util.getCredProvider(Util.java:41)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at network.NetworkService.onCreate(NetworkService.java:55)
08-05 02:21:24.908: E/AndroidRuntime(25712):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2657)
08-05 02:21:24.908: E/AndroidRuntime(25712):    ... 10 more

注释掉该行可以正常运行应用程序。但我没有看到上传到 Amazon S3 的文件。我确实设置了 Amazon Cognito 凭据提供程序。

源码可以在here找到。

【问题讨论】:

【参考方案1】:

CognitoCredentialsProvider.refresh() 将发出网络请求以获取 Cognito 身份 ID 和会话凭据,如果这些凭据没有被缓存(由提供者自己处理)。根据堆栈跟踪,您在主线程上调用 getCredProvider(Context context)。并在主线程上调用 refresh()。这就是您看到NetworkOnMainThreadException 的原因。要解决此问题,您可以删除 sCredProvider.refresh()。如果由于某种原因你想调用 refresh(),你需要在后台线程中调用它。请参考AsyncTask。

【讨论】:

以上是关于刷新时 CognitoCredentialsProvider 抛出 runtimeException的主要内容,如果未能解决你的问题,请参考以下文章

刷新物化视图时,索引是不是也会自动刷新?

UIRefreshControl:刷新时 UITableView '卡住'

JS怎么实现页面刷新时,页面中的iframe不刷新

problem:vue组件局部刷新,在组件销毁(destroyed)时取消刷新无效问题

单击按钮刷新时如何刷新小部件列表视图?

当我使用旧的刷新令牌时,为啥刷新端点会返回新的令牌?