我的代码如下:
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)。所以我一点也不需要那个管理员。但我也不希望每次都将错误消息写入我的日志。另外,我也不想使用这种只抓取一个实例而不做任何事情的黑客解决方案。
这个处理程序对我的案子有什么用?
最佳答案
我阅读了release v2.11.0和release v2.11.1(23小时前发布)变更日志,它们指示下一个:
Amazon S3(增强功能):
介绍了TransferNetworkLosshandler,它是一个监听网络连接更改的实用程序。TransferNetworkLosshandler在网络脱机时暂停正在进行的传输,并恢复在网络重新联机时暂停的传输。
Amazon S3(错误修复):
修正了transferutility中的一个错误,在传输执行期间,当网络脱机时,状态未设置为“等待网络”。
在您的情况下,您可以包括transferNetworkLosshandler以避免错误消息,并使用transferListener检测到状态更改为“waiting\u for\u 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);
}
}