Alamofire API请求因responseSerializationFailed在Swift 3中失败

编程入门 行业动态 更新时间:2024-10-26 12:27:42
本文介绍了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中失败

本文发布于:2023-11-26 03:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:API   Alamofire   Swift   responseSerializationFailed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!