[英]Error Alamofire 5 response success and response failure
I was using Alamofire with version 4 and it was working very well.我使用的是版本 4 的 Alamofire,它运行良好。 When I update Alamofire to version 5, it gives me an error getting the result response ".success" or ".failure"当我将 Alamofire 更新到版本 5 时,我在获取结果响应“.success”或“.failure”时出现错误
How can I get the result response ".success" or ".failure" with Alamofire 5?如何使用 Alamofire 5 获得结果响应“.success”或“.failure”?
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData!, withName: "signature", fileName: String("signature"), mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append((value.data(using: String.Encoding.utf8)!), withName: key)
}
},
to: URL,
method: .post,
headers: headers) { (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print("Succes: ", response.result.value ?? "")
if let data = response.data {
do {
let decoder = JSONDecoder()
let model = try decoder.decode(T.self, from: data)
success(model)
} catch let error as NSError {
print(error)
}
}
}
case .failure(let encodingError):
print("error: ", encodingError)
}
}
In Alamofire 5.0 why you need success and failure?在 Alamofire 5.0 中,为什么需要成功和失败? i am achieving it with code below我用下面的代码实现它
static func uploadFile(url: String, fileData: Data?, parameters: [String : String], type: MessageType!, fileName: String!, completionHandler: @escaping (Result<Data, Error>) -> Void) {
_ = AF.upload(multipartFormData: { (multiFormData) in
var fileType: String!
if type! == MessageType.Video {
fileType = "video/mp4"
} else if type! == MessageType.Image {
fileType = "image/jpeg"
}
multiFormData.append(fileData!, withName: "uploadedfile", fileName: fileName , mimeType: fileType)
for (key, value) in parameters {
multiFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to: url).uploadProgress(queue: .main, closure: { progress in
print("Upload Progress: \(progress.fractionCompleted)")
}).responseJSON(completionHandler: { response in
if response.error != nil {
completionHandler(.failure(response.error!))
} else {
completionHandler(.success(response.data!))
}
})
}
I got the same issue few days ago, here is the working code for this.几天前我遇到了同样的问题,这里是这个的工作代码。
pod 'Alamofire', '5.1.0'吊舱'Alamofire','5.1.0'
AF.upload(multipartFormData: { (mulipartFormData) in
for (key, value) in parameters {
mulipartFormData.append(value.data(using: .utf8)!, withName: key)
}
for (image, key) in images {
let imgData = image.jpegData(compressionQuality: compression)!
mulipartFormData.append(imgData, withName: key, fileName: "\(key).jpeg", mimeType: "image/jpeg")
}
}, to: url, method: .post, headers: HTTPHeaders(headers)).response { response in
onComplete(response.data, response.response, response.error)
}.uploadProgress { progress in
print("progress: \(progress.fractionCompleted * 100)")
onProgress?(progress)
}
To determine if request is successful, with AF 5 you can do this like that in the response block (response, responseData, responseJSON, ...):要确定请求是否成功,使用 AF 5,您可以像在响应块(response、responseData、responseJSON、...)中那样执行此操作:
switch response.result {
case .success:
// get the data
case .failure:
// error
}
OR just check:或者只是检查:
response.value != nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.