更新的问题:
起初,我的问题是要根据ParameterEncoding
的请求为每个ViewController
分配一个自定义HTTP.Method
,我找到了一个解决方案,并在Alamofire.request
中添加了字符串,然后添加了import Alamofire
到每个ViewController
的头部,这有助于调用JSONEncoding
和URLEncoding
,并且通过将标头添加到请求中来更改了调用方式,从而避免了401未经授权的错误。
func callingHttpRequest(params:Dictionary<String,Any>, apiname:String,cuurentView:UIViewController,method:HTTPMethod, taskCallback: @escaping (Int,
AnyObject?) -> Void) {
let urlString = HOST_NAME + apiname
print("url",urlString)
print("params", params)
Alamofire.request(urlString,method: method,parameters:params).validate().responseJSON { response in
switch response.result {
case .success(let resultData):
taskCallback(1,resultData as AnyObject)
let returnData = String(data: response.data! , encoding: .utf8)
print("returnData" ,returnData!)
print("request URL", response.request!)
break
case .failure(let error):
let returnData = String(data: response.data! , encoding: .utf8)
print("returnData" ,returnData!)
print("request URL", response.request!)
if !Connectivity.isConnectedToInternet(){
NetworkManager.sharedInstance.dismissLoader()
cuurentView.view.isUserInteractionEnabled = true
let AC = UIAlertController(title: "Warning", message: error.localizedDescription, preferredStyle: .alert)
let okBtn = UIAlertAction(title: "Retry", style: .default, handler: {(_ action: UIAlertAction) -> Void in
taskCallback(2, "" as AnyObject)
})
let noBtn = UIAlertAction(title: "Cancel", style: .destructive, handler: {(_ action: UIAlertAction) -> Void in
})
AC.addAction(okBtn)
AC.addAction(noBtn)
cuurentView.present(AC, animated: true, completion: { _ in })
}
else{
let errorCode:Int = error._code;
if errorCode != -999 && errorCode != -1005{
NetworkManager.sharedInstance.dismissLoader()
cuurentView.view.isUserInteractionEnabled = true
let AC = UIAlertController(title: "Warning", message: error.localizedDescription, preferredStyle: .alert)
let okBtn = UIAlertAction(title: "Retry", style: .default, handler: {(_ action: UIAlertAction) -> Void in
taskCallback(2, "" as AnyObject)
})
let noBtn = UIAlertAction(title: "Cancel", style: .destructive, handler: {(_ action: UIAlertAction) -> Void in
})
AC.addAction(okBtn)
AC.addAction(noBtn)
cuurentView.present(AC, animated: true, completion: { _ in })
}else if errorCode == -1005{
NetworkManager.sharedInstance.dismissLoader()
taskCallback(2, "" as AnyObject)
}
}
break;
}
}
现在真正让我烦恼的是请求的响应,该应用是使用用户登录名创建的,因此每次运行该应用程序时,它都会检查用户是否登录。发送HTTP请求后,如果用户按照以下方式登录,则返回的响应将SQL Query
添加到JSON
失败returnData字符串(84)“ SELECT * FROM customer,其中LOWER(user)='helloworld'AND status ='1'” {“成功”:true,“数据”:[{,,,,“}
这导致故障响应和错误
警告:由于错误而无法序列化JSON:由于数据格式不正确,因此无法读取数据。
这真是太疯狂了,为什么响应在不应该返回SQL Query
的情况下返回。在我自定义Alamofire.request
的标头和编码之前,这是没有发生的。
任何面对这种问题的方法都可以帮助!
P.S。:响应具有200 OK HTTP响应,但数据响应失败