在我的应用程序中,我使用以下代码通过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);
图片上传工作正常。但是当我开始图像上传和网络断开时,我无法获得响应。在这种情况下我怎么会失败?
答案 0 :(得分:2)
调用uploadFileInterface.uploadFile()时可以捕获RetrofitError:
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
}