alamofire 分段上传请求的完成处理程序
completion handler at alamofire multipart upload request
我在 swift 中获取数据 (JSON) 时遇到问题。我知道它的完成处理,但我正在努力,所以也许有人可以提供帮助。
下面的代码显示了我的功能。 API 需要 json 作为字段中的字符串。请求本身有效 > print(post) 显示正确的数据。但我不明白那个功能。
(我使用 Alamofire、SwiftyJSON、swift2.2 和 xcode7.3)
class func searchCities(jsonStr: String) -> JSON {
let oEndpoint = "https://api.domain.com/api/1.0/"
guard let url = NSURL(string: oEndpoint) else {
print("error url")
return nil
}
// let parameters : [String: String] = ["one":"some param", "two":"some other param"]
var result: JSON = JSON("")
Alamofire.upload(.POST,
url,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: jsonStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"data")
// parameters if needed
// multipartFormData.appendBodyPart(data: value, name: key)
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON{ response in
guard response.result.error == nil else {
print("error response")
print(response.result.error!)
return
}
if let value: AnyObject = response.result.value{
let post = JSON(value)
print(post)
result = JSON(value) }
}
case .Failure( _):
print("failure")
break
}
}
)
//print("res: \(result)")
return result
}
所以我想明白了...
class func searchFeeds(jsonStr: String, completion: (response: JSON) -> Void) {
let oEndpoint = "https://api.domain.com/api/1.0/"
guard let url = NSURL(string: oEndpoint) else {
print("error url")
completion(response: nil)
return
}
Alamofire.upload(.POST,
url,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: jsonStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"data")
// parameters if needed
// multipartFormData.appendBodyPart(data: value, name: key)
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON{ response in
guard response.result.error == nil else {
print("error response")
print(response.result.error!)
completion(response: nil)
return
}
if let value: AnyObject = response.result.value{
completion(response: JSON(value))
}
}
case .Failure( _):
print("failure")
break
}
}
)
}
然后用...得到结果
APIclass.searchFeeds(jsonString) {
(response: JSON) in
print(response)
}
不完美,但它有效。
我在 swift 中获取数据 (JSON) 时遇到问题。我知道它的完成处理,但我正在努力,所以也许有人可以提供帮助。
下面的代码显示了我的功能。 API 需要 json 作为字段中的字符串。请求本身有效 > print(post) 显示正确的数据。但我不明白那个功能。 (我使用 Alamofire、SwiftyJSON、swift2.2 和 xcode7.3)
class func searchCities(jsonStr: String) -> JSON {
let oEndpoint = "https://api.domain.com/api/1.0/"
guard let url = NSURL(string: oEndpoint) else {
print("error url")
return nil
}
// let parameters : [String: String] = ["one":"some param", "two":"some other param"]
var result: JSON = JSON("")
Alamofire.upload(.POST,
url,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: jsonStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"data")
// parameters if needed
// multipartFormData.appendBodyPart(data: value, name: key)
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON{ response in
guard response.result.error == nil else {
print("error response")
print(response.result.error!)
return
}
if let value: AnyObject = response.result.value{
let post = JSON(value)
print(post)
result = JSON(value) }
}
case .Failure( _):
print("failure")
break
}
}
)
//print("res: \(result)")
return result
}
所以我想明白了...
class func searchFeeds(jsonStr: String, completion: (response: JSON) -> Void) {
let oEndpoint = "https://api.domain.com/api/1.0/"
guard let url = NSURL(string: oEndpoint) else {
print("error url")
completion(response: nil)
return
}
Alamofire.upload(.POST,
url,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: jsonStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"data")
// parameters if needed
// multipartFormData.appendBodyPart(data: value, name: key)
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON{ response in
guard response.result.error == nil else {
print("error response")
print(response.result.error!)
completion(response: nil)
return
}
if let value: AnyObject = response.result.value{
completion(response: JSON(value))
}
}
case .Failure( _):
print("failure")
break
}
}
)
}
然后用...得到结果
APIclass.searchFeeds(jsonString) {
(response: JSON) in
print(response)
}
不完美,但它有效。