6

我的代码如下所示:

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)。所以我根本不需要处理程序。但我也不希望每次都将错误消息写入我的日志。此外,我不想使用这种只抓取一个实例而不用它做任何事情的骇人听闻的解决方案。

对于我的情况,这个处理程序的正确用途是什么?

4

2 回答 2

2

我阅读了 release_v2.11.0 和 release_v2.11.1(23 小时前发布)更改日志,它们指示下一个:

  • Amazon 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);
    }
}
于 2019-02-05T18:09:38.550 回答
0

您可以在此处查看这些链接:

Java https://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html

.NET https://docs.aws.amazon.com/AmazonS3/latest/dev/HLuploadFileDotNet.html

希望能帮助到你

于 2019-02-01T17:47:56.050 回答