本文介绍了Alamofire API请求因responseSerializationFailed在Swift 3中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Alamofire调用普通的API调用,并且没有在网络管理器类中传递任何数据。
I am calling normal API call with Alamofire, And I am not passing any data with that in networking manager class.
我的网络类是
func executeGetRequest(url:String,requestVC:UIViewController,completionHandler:@escaping (_ responseObject:Any?) -> Void!,failureHandler:@escaping (_ connectionError:NSError?) -> Void!){ //Checking internet alert if !self.isConnectedToInternet(){ // requestVC.showAlert(kText_AppName, message: kText_NoInternet) return } requestVC.showLoader() Alamofire.request(url).responseJSON { (response:DataResponse) in requestVC.removeLoader() switch(response.result) { case .success(_): if response.result.value != nil{ completionHandler (response.result.value) } break case .failure(let error): failureHandler (error as NSError?) break } } }我是从我的主要班级来称呼它的
and i am calling it from my main class
kNetworkManager.executeGetRequest(url: kAppAccessTokenURL, requestVC: self, completionHandler: { (responseObject) -> () in print("responseObject:\(responseObject!)") }, failureHandler: {(error)-> () in print("response object:\(error!)") self.showAlert(message: (error?.description)!, title: kText_AppName) if error?._code == NSURLErrorTimedOut { //timeout here self.showAlert(message: kText_timeout, title: kText_AppName) } })总是获取请求失败,并显示错误为 responseSerializationFailed
Its getting always request fail and showing error as responseSerializationFailed
如果我直接在Main class中直接调用,而没有
if I call directly in Main Class without manager class like
Alamofire.request(kAppAccessTokenURL).responseString { response in中的响应
我能够获得响应,任何人都可以建议我在网络课程中哪里出错了。
I am able to getting response, can anyone suggest me where getting wrong in Network class.
推荐答案在这里
Alamofire.request(kAppAccessTokenURL).responseString还有
Alamofire.request(url).responseJSON期待那个
let jsonText = "{\"first_name\":\"Sergey\"}" var dictonary:NSDictionary? if let data = jsonText.dataUsingEncoding(NSUTF8StringEncoding) { do { dictonary = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject] if let myDictionary = dictonary { print(" First name is: \(myDictionary["first_name"]!)") } } catch let error as NSError { print(error) } }更多推荐
Alamofire API请求因responseSerializationFailed在Swift 3中失败
发布评论