Android Retrofit RestAdapter 在断开连接时获得失败状态

标签 android amazon-web-services retrofit

在我的应用程序中,我使用以下代码通过 Retrofit 将图像上传到 AWS:

private interface UploadFileToAwsInterface {
        @Multipart
        @POST("/")
        SuccessResponse uploadFile(@Part("key") String key,
                                   @Part("AWSAccessKeyId") String AWSAccessKeyId,
                                   @Part("acl") String acl,
                                   @Part("success_action_redirect") String success_action_redirect,
                                   @Part("policy") String policy,
                                   @Part("signature") String signature,
                                   @Part("utf8") String utf8,
                                   @Part("Content-Type") String contType,
                                   @Part("file") TypedFile file);
    }

    public Boolean uploadFileToAws(AWSCredentials awsCredentials, File localFile, String mimeType) {
        TypedFile file = new TypedFile(mimeType, localFile);
        endPoint.setUrl(awsCredentials.getAction());
        UploadFileToAwsInterface uploadFileInterface = restAdapter.create(UploadFileToAwsInterface.class);
        SuccessResponse response = uploadFileInterface.uploadFile(
                awsCredentials.getKey(),
                awsCredentials.getAWSAccessKeyId(),
                awsCredentials.getAcl(),
                awsCredentials.getSuccess_action_redirect(),
                awsCredentials.getPolicy(),
                awsCredentials.getSignature(),
                "true",
                mimeType,
                file);

图片上传正常。但是当我开始图片上传和网络断开时,我无法得到响应。在这种情况下我怎么会失败?

最佳答案

你可以捕获 RetrofitError当您调用 uploadFileInterface.uploadFile() 时:

try {
  SuccessResponse response = uploadFileInterface.uploadFile(
      awsCredentials.getKey(),
      awsCredentials.getAWSAccessKeyId(),
      awsCredentials.getAcl(),
      awsCredentials.getSuccess_action_redirect(),
      awsCredentials.getPolicy(),
      awsCredentials.getSignature(), 
      "true", 
      mimeType, 
      file);
} catch (RetrofitError error) {
  //TODO Show exception here
}

关于Android Retrofit RestAdapter 在断开连接时获得失败状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29250346/

相关文章:

java - 在 Android 中使用 httpUrlConnection 发送授权 header

sockets - 如何从 AWS EC2 实例 ping 到我的本地机器?

ruby-on-rails - 回形针 aws-sdk 错误 : uninitialized constant

ios - Swift 4 - 尝试在 AVPlayerViewController 中从后端显示 AWS 视频 url 不起作用,出现奇怪的错误

java - 使用 OkHttp3/Retrofit 添加 cookie 到响应

android - 为什么在 Android doOnError() 上使用 Retrofit 的 RxJava 不起作用但订阅者 onError 可以

android - Paypal for android 如何在回调中识别付款项?

android - 无法创建文件夹和文件

kotlin - moshi自定义限定符批注仅在一个属性上序列化null

android - ToggleButton isChecked 与 isActivated - Android