简体   繁体   English

AWS S3 Android SDK 2.11.0

[英]AWS S3 Android SDK 2.11.0

My code looks like this: 我的代码看起来像这样:

final AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(s3AccessKeyId, s3SecretAccessKey));
final TransferUtility util = TransferUtility.
                    builder().s3Client(s3Client).context(context).build();

When upgrading from 'com.amazonaws:aws-android-sdk-s3:2.7.5' to 'com.amazonaws:aws-android-sdk-s3:2.11.0' I get the following error in my logcat, even though the upload is still working: 从'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]

When I change my code to include the TransferNetworkLossHandler: 当我更改我的代码以包含TransferNetworkLossHandler时:

TransferNetworkLossHandler.getInstance(context);
final AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(s3AccessKeyId, s3SecretAccessKey));
final TransferUtility util = TransferUtility.
                    builder().s3Client(s3Client).context(context).build();

without doing anything with it, I get no more error. 没有做任何事情,我没有更多的错误。

However, this is not how this error message is intended. 但是,这不是这个错误消息的意图。 It tells you to use the TransferNetworkLossHandler, but there is no documentation on how to use it. 它告诉您使用TransferNetworkLossHandler,但没有关于如何使用它的文档。

I just want to display an error (like a Toast or an AlertDialog) to the user, if the upload did not work. 我只想向用户显示错误(如Toast或AlertDialog),如果上传不起作用。 So I don't need the handler at all. 所以我根本不需要处理程序。 But I also don't want the error message to be written into my log everytime. 但我也不希望每次都将错误消息写入我的日志。 Also I don't want to use this hackish solution of just grabbing an instance and not doing anything with it. 此外,我不想使用这个只是抓住一个实例而不用它做任何事情的hackish解决方案。

What is the proper use of this handler for my case? 这个处理程序对我的案例有什么用处?

I read release_v2.11.0 and release_v2.11.1 (released 23hs ago) CHANGELOGS and they indicate the next: 我阅读了release_v2.11.0和release_v2.11.1(23小时前发布)CHANGELOGS,他们指出了下一个:

  • Amazon S3 (Enhancements): Introduced TransferNetworkLossHandler, a utility that listens for network connectivity changes. Amazon S3(增强功能):推出TransferNetworkLossHandler,一种侦听网络连接变化的实用程序。 TransferNetworkLossHandler pauses the on-going transfers when the network goes offline and resumes the transfers that were paused when the network comes back online. 当网络脱机时,TransferNetworkLossHandler会暂停正在进行的传输,并恢复网络重新联机时暂停的传输。

  • Amazon S3 (Bug fixes): Fixed a bug in TransferUtility where the state is not set to 'WAITING_FOR_NETWORK' when network goes offline during execution of transfers. Amazon S3(错误修复):修复了TransferUtility中的一个错误,当传输执行期间网络脱机时,状态未设置为“WAITING_FOR_NETWORK”。

In your case u can include TransferNetworkLossHandler to avoid the error message and use TransferListener to detect state change to 'WAITING_FOR_NETWORK', 'FAILED' or 'ERROR' and notice user: 在您的情况下,您可以包含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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM