Alamofire 请求未在 swift 5 中执行
Alamofire request is not executed in swift 5
我在整个项目中一直在使用 Alamofire。现在在我的一个模块中,Alamofire.request() 没有被执行。我尝试放置一个断点,它显示突然间请求被跳过。由于出于安全目的我正在从事一个实时项目,因此不包括 URL。我使用的代码在这里给出。
func responseFunction() {
let url = ""
let parametervalue = ["enq_id" : self.enquiry_id]
print(self.enquiry_id)
Alamofire.request(url, method: .post, parameters: parametervalue).responseJSON(completionHandler: {response in
if let response_data = response.result.value as? NSDictionary{
let rdata = response_data.value(forKey: "response") as! String
if rdata == "Success"
{
let res_data = response_data.value(forKey: "result") as! [NSDictionary]
print(res_data)
for resultdata in res_data{
let enqid = resultdata.value(forKey: "enq_id") as! String
let userid = resultdata.value(forKey: "user_id") as! String
let reference_no = resultdata.value(forKey: "reference_no") as! String
let enq_date = resultdata.value(forKey: "enq_date") as! String
let enq_time1 = resultdata.value(forKey: "enq_time") as! String
let enq_time2 = enq_time1.replacingOccurrences(of: "+", with: " ")
let enq_time = enq_time2.replacingOccurrences(of: "%3A", with: " : ")
}
self.queryResponseTable.reloadData()
}
else{
let alertController = UIAlertController(title: "Response Details", message: "Server Error ! Could not get any response. Please try again later!", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .default))
self.present(alertController, animated: true, completion: nil)
}
}
})
}
谁能帮帮我?
我认为下面的代码对你有帮助。
func Api_withParameters(aView : UIView, aWebServiceName : String, aDictParam : Dictionary<String, Any>, completeBlock: @escaping ( _ response: AnyObject) -> Void, errorBlock: @escaping ( _ error: AnyObject) -> Void) {
let passingURL = "\(BASEURL)/\(aWebServiceName)"
let jsonData = try? JSONSerialization.data(withJSONObject: aDictParam, options: [])
var jsonString = String(data: jsonData!, encoding: .utf8)
jsonString = jsonString!.filter { !"\)(\n\t\r".contains([=10=]) }
let passingParameter = [K_APIKEY:APIKEY, K_Data:jsonString!]
print("***** Web-Service URL : \(passingURL) \n Passing Parameter : \(passingParameter)")
Alamofire.request(passingURL, method: HTTPMethod.post, parameters: passingParameter, encoding: URLEncoding.default, headers: HEADER_CONTENT_TYPE).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
completeBlock(response.result.value as AnyObject)
break
case .failure(let error):
var errorDict = [String : Any]()
let errorcode : Int = error._code
if let httpStatusCode = response.response?.statusCode {
switch(httpStatusCode) {
case 404:
errorDict = ["errormsg" : "Invalid URL: \(passingURL)", "errorCode" : httpStatusCode]
default: break
}
} else {
if (String(errorcode) == "-1009"){
errorDict = ["errormsg" : "Please check your internet connection"]
}
else if(error.localizedDescription != ""){
errorDict = ["errormsg" : error.localizedDescription]
}
else{
errorDict = ["errormsg" : "Server unreachable please try again later."]
}
}
errorBlock(errorDict as AnyObject)
break
}
}
}
我在整个项目中一直在使用 Alamofire。现在在我的一个模块中,Alamofire.request() 没有被执行。我尝试放置一个断点,它显示突然间请求被跳过。由于出于安全目的我正在从事一个实时项目,因此不包括 URL。我使用的代码在这里给出。
func responseFunction() {
let url = ""
let parametervalue = ["enq_id" : self.enquiry_id]
print(self.enquiry_id)
Alamofire.request(url, method: .post, parameters: parametervalue).responseJSON(completionHandler: {response in
if let response_data = response.result.value as? NSDictionary{
let rdata = response_data.value(forKey: "response") as! String
if rdata == "Success"
{
let res_data = response_data.value(forKey: "result") as! [NSDictionary]
print(res_data)
for resultdata in res_data{
let enqid = resultdata.value(forKey: "enq_id") as! String
let userid = resultdata.value(forKey: "user_id") as! String
let reference_no = resultdata.value(forKey: "reference_no") as! String
let enq_date = resultdata.value(forKey: "enq_date") as! String
let enq_time1 = resultdata.value(forKey: "enq_time") as! String
let enq_time2 = enq_time1.replacingOccurrences(of: "+", with: " ")
let enq_time = enq_time2.replacingOccurrences(of: "%3A", with: " : ")
}
self.queryResponseTable.reloadData()
}
else{
let alertController = UIAlertController(title: "Response Details", message: "Server Error ! Could not get any response. Please try again later!", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .default))
self.present(alertController, animated: true, completion: nil)
}
}
})
}
谁能帮帮我?
我认为下面的代码对你有帮助。
func Api_withParameters(aView : UIView, aWebServiceName : String, aDictParam : Dictionary<String, Any>, completeBlock: @escaping ( _ response: AnyObject) -> Void, errorBlock: @escaping ( _ error: AnyObject) -> Void) {
let passingURL = "\(BASEURL)/\(aWebServiceName)"
let jsonData = try? JSONSerialization.data(withJSONObject: aDictParam, options: [])
var jsonString = String(data: jsonData!, encoding: .utf8)
jsonString = jsonString!.filter { !"\)(\n\t\r".contains([=10=]) }
let passingParameter = [K_APIKEY:APIKEY, K_Data:jsonString!]
print("***** Web-Service URL : \(passingURL) \n Passing Parameter : \(passingParameter)")
Alamofire.request(passingURL, method: HTTPMethod.post, parameters: passingParameter, encoding: URLEncoding.default, headers: HEADER_CONTENT_TYPE).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
completeBlock(response.result.value as AnyObject)
break
case .failure(let error):
var errorDict = [String : Any]()
let errorcode : Int = error._code
if let httpStatusCode = response.response?.statusCode {
switch(httpStatusCode) {
case 404:
errorDict = ["errormsg" : "Invalid URL: \(passingURL)", "errorCode" : httpStatusCode]
default: break
}
} else {
if (String(errorcode) == "-1009"){
errorDict = ["errormsg" : "Please check your internet connection"]
}
else if(error.localizedDescription != ""){
errorDict = ["errormsg" : error.localizedDescription]
}
else{
errorDict = ["errormsg" : "Server unreachable please try again later."]
}
}
errorBlock(errorDict as AnyObject)
break
}
}
}