ossAndroid为真A.11.0
AWS S3 Android SDK 2.11.0
我的代码如下所示:
final AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(s3AccessKeyId, s3SecretAccessKey));
final TransferUtility util = TransferUtility.
builder().s3Client(s3Client).context(context).build();
从 'com.amazonaws:aws-android-sdk-s3:2.7.5' 升级到 'com.amazonaws:aws-android-sdk-s3:2.11.0' 时,我的 logcat 中出现以下错误,尽管上传仍在进行:
E/nsferNetworkLossHandler: TransferNetworkLossHandler is not created. Please call `TransferNetworkLossHandler.getInstance(Context)` to instantiate it before retrieving
E/UploadTask: TransferUtilityException: [com.amazonaws.mobileconnectors.s3.transferutility.TransferUtilityException: TransferNetworkLossHandler is not created. Please call `TransferNetworkLossHandler.getInstance(Context)` to instantiate it before retrieving]
当我更改我的代码以包含 TransferNetworkLossHandler 时:
TransferNetworkLossHandler.getInstance(context);
final AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(s3AccessKeyId, s3SecretAccessKey));
final TransferUtility util = TransferUtility.
builder().s3Client(s3Client).context(context).build();
无需对其进行任何操作,我不会再收到错误。
但是,这不是此错误消息的本意。它告诉你使用 TransferNetworkLossHandler,但没有关于如何使用它的文档。
我只想在上传失败时向用户显示错误(如 Toast 或 AlertDialog)。所以我根本不需要处理程序。但我也不希望每次都将错误消息写入我的日志中。此外,我不想使用这种只获取实例而不对其进行任何操作的骇人听闻的解决方案。
对于我的案例,此处理程序的正确用法是什么?
您可以在此处查看这些链接:
Java https://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html
.NET https://docs.aws.amazon.com/AmazonS3/latest/dev/HLuploadFileDotNet.html
希望对您有所帮助
我阅读了 release_v2.11.0 和 release_v2.11.1(23 小时前发布)CHANGELOGS,它们指出了下一个:
亚马逊 S3(增强):
引入了 TransferNetworkLossHandler,这是一种用于侦听网络连接变化的实用程序。 TransferNetworkLossHandler 在网络离线时暂停正在进行的传输,并在网络恢复在线时恢复暂停的传输。
Amazon S3(错误修复):
修复了 TransferUtility 中的一个错误,该错误在执行传输期间网络离线时状态未设置为 'WAITING_FOR_NETWORK'。
在您的情况下,您可以包含 TransferNetworkLossHandler 以避免错误消息,并使用 TransferListener 检测状态更改为 'WAITING_FOR_NETWORK'、'FAILED' 或 'ERROR' 并通知用户:
TransferObserver transferObserver;
transferObserver = transferUtility.upload(UPLOAD_FOLDER+fileName, fileToUpload);
transferObserver.setTransferListener(new UploadListener());
/**
* Upload listener to check transfer states and progress
*/
private class UploadListener implements TransferListener {
@Override
public void onStateChanged(int id, TransferState state) {
Log.d(TAG, "onStateChanged: " + id + ", " + state.toString());
// If upload error, failed or network disconnect
if(state == TransferState.ERROR || state == TransferState.FAILED || state == TransferState.WAITING_FOR_NETWORK){
// HERE end service and notice user !!!
}
}
@Override public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
float percentDonef = ((float) bytesCurrent / (float) bytesTotal) * 100; int percentDone = (int)percentDonef;
Log.d(TAG, "ID:" + id + " bytesCurrent: " + bytesCurrent + " bytesTotal: " + bytesTotal + " " + percentDone + "%");
}
@Override public void onError(int id, Exception ex) {
Log.e(TAG, ex);
}
}
我的代码如下所示:
final AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(s3AccessKeyId, s3SecretAccessKey));
final TransferUtility util = TransferUtility.
builder().s3Client(s3Client).context(context).build();
从 'com.amazonaws:aws-android-sdk-s3:2.7.5' 升级到 'com.amazonaws:aws-android-sdk-s3:2.11.0' 时,我的 logcat 中出现以下错误,尽管上传仍在进行:
E/nsferNetworkLossHandler: TransferNetworkLossHandler is not created. Please call `TransferNetworkLossHandler.getInstance(Context)` to instantiate it before retrieving
E/UploadTask: TransferUtilityException: [com.amazonaws.mobileconnectors.s3.transferutility.TransferUtilityException: TransferNetworkLossHandler is not created. Please call `TransferNetworkLossHandler.getInstance(Context)` to instantiate it before retrieving]
当我更改我的代码以包含 TransferNetworkLossHandler 时:
TransferNetworkLossHandler.getInstance(context);
final AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(s3AccessKeyId, s3SecretAccessKey));
final TransferUtility util = TransferUtility.
builder().s3Client(s3Client).context(context).build();
无需对其进行任何操作,我不会再收到错误。
但是,这不是此错误消息的本意。它告诉你使用 TransferNetworkLossHandler,但没有关于如何使用它的文档。
我只想在上传失败时向用户显示错误(如 Toast 或 AlertDialog)。所以我根本不需要处理程序。但我也不希望每次都将错误消息写入我的日志中。此外,我不想使用这种只获取实例而不对其进行任何操作的骇人听闻的解决方案。
对于我的案例,此处理程序的正确用法是什么?
您可以在此处查看这些链接:
Java https://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html
.NET https://docs.aws.amazon.com/AmazonS3/latest/dev/HLuploadFileDotNet.html
希望对您有所帮助
我阅读了 release_v2.11.0 和 release_v2.11.1(23 小时前发布)CHANGELOGS,它们指出了下一个:
亚马逊 S3(增强): 引入了 TransferNetworkLossHandler,这是一种用于侦听网络连接变化的实用程序。 TransferNetworkLossHandler 在网络离线时暂停正在进行的传输,并在网络恢复在线时恢复暂停的传输。
Amazon S3(错误修复): 修复了 TransferUtility 中的一个错误,该错误在执行传输期间网络离线时状态未设置为 'WAITING_FOR_NETWORK'。
在您的情况下,您可以包含 TransferNetworkLossHandler 以避免错误消息,并使用 TransferListener 检测状态更改为 'WAITING_FOR_NETWORK'、'FAILED' 或 'ERROR' 并通知用户:
TransferObserver transferObserver;
transferObserver = transferUtility.upload(UPLOAD_FOLDER+fileName, fileToUpload);
transferObserver.setTransferListener(new UploadListener());
/**
* Upload listener to check transfer states and progress
*/
private class UploadListener implements TransferListener {
@Override
public void onStateChanged(int id, TransferState state) {
Log.d(TAG, "onStateChanged: " + id + ", " + state.toString());
// If upload error, failed or network disconnect
if(state == TransferState.ERROR || state == TransferState.FAILED || state == TransferState.WAITING_FOR_NETWORK){
// HERE end service and notice user !!!
}
}
@Override public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
float percentDonef = ((float) bytesCurrent / (float) bytesTotal) * 100; int percentDone = (int)percentDonef;
Log.d(TAG, "ID:" + id + " bytesCurrent: " + bytesCurrent + " bytesTotal: " + bytesTotal + " " + percentDone + "%");
}
@Override public void onError(int id, Exception ex) {
Log.e(TAG, ex);
}
}