I am trying to upload a text file to Amazon s3 from my android application. I am using the following code snippet provided by Amazon AWS.
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;
}
When the code passes through sCredProvider.refresh(); it throws the follow error.
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
Commenting out the line run the application fine. But I don't see the file uploaded to Amazon S3. I did setup to Amazon Cognito credential provider.
The source code can be found here.