向后端循环 JSON 请求(REST API)
Loop JSON requests to backend (REST API)
我正在向后端发送请求 (REST API)。我的任务:如果 JSON 响应没有以要求的格式出现,或者 returns 出现错误,则开始请求周期 - 3 次尝试,如果这 3 次尝试都失败,则退出,否则继续下一步(首页)
代码如下:
func checkUsers(){
let headers: HTTPHeaders = ["Content-Type" : "application/json"]
NetworkManager.sharedInstance.manager.request("https://restapi.com/user",
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: headers).responseJSON { (response) -> Void in
switch response.result {
case .success(let value):
let response = JSON(value)
print("JSON Response: \(response)")
if response["operation"].stringValue == "loginerror" {
print("Error")
self.showNotRegisteredScreen()
} else {
usersFIO = response["userName"].stringValue
self.didFinishLogin()
}
case .failure(let error):
print("fail to fecth data",error)
self.showErrorScreen()
}
}
请告诉我如何开始指定三次尝试的请求周期
P.S.: 使用 Alamofire 和 SwiftyJson pods
提前致谢!
它适用于我的 Alamofire 5
lazy var manager: Alamofire.Session = {
let configuration = URLSessionConfiguration.default
let retryPolicy = RetryPolicy(retryLimit: 3)
let interceptor = Interceptor(retriers: [retryPolicy])
let manager = Alamofire.Session(configuration: configuration, interceptor: interceptor, serverTrustManager: serverTrustManager, eventMonitors: [CertificateAuthAdapter()])
return manager
}()
我正在向后端发送请求 (REST API)。我的任务:如果 JSON 响应没有以要求的格式出现,或者 returns 出现错误,则开始请求周期 - 3 次尝试,如果这 3 次尝试都失败,则退出,否则继续下一步(首页)
代码如下:
func checkUsers(){
let headers: HTTPHeaders = ["Content-Type" : "application/json"]
NetworkManager.sharedInstance.manager.request("https://restapi.com/user",
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: headers).responseJSON { (response) -> Void in
switch response.result {
case .success(let value):
let response = JSON(value)
print("JSON Response: \(response)")
if response["operation"].stringValue == "loginerror" {
print("Error")
self.showNotRegisteredScreen()
} else {
usersFIO = response["userName"].stringValue
self.didFinishLogin()
}
case .failure(let error):
print("fail to fecth data",error)
self.showErrorScreen()
}
}
请告诉我如何开始指定三次尝试的请求周期
P.S.: 使用 Alamofire 和 SwiftyJson pods
提前致谢!
它适用于我的 Alamofire 5
lazy var manager: Alamofire.Session = {
let configuration = URLSessionConfiguration.default
let retryPolicy = RetryPolicy(retryLimit: 3)
let interceptor = Interceptor(retriers: [retryPolicy])
let manager = Alamofire.Session(configuration: configuration, interceptor: interceptor, serverTrustManager: serverTrustManager, eventMonitors: [CertificateAuthAdapter()])
return manager
}()