【发布时间】:2018-12-18 01:01:05
【问题描述】:
为了在此处获取推送通知,我使用 alamofire post 方法(pod 版本 alamofire 4.5)发送 postitem、token、count 和 currentname。调用 post 方法时我没有得到任何响应,并且它没有显示任何错误。 我尝试在 alamofire 函数中保留断点,它调用 alamofire.requestion 然后它退出函数。
这是尝试将 post 方法发送到后端的代码:
func postNotification(postItem: String, post: Post) {
print("Get token from post:::",post.token)
print(postItem)
let token = UserDefaults.standard.string(forKey: "token")
let headers: HTTPHeaders = ["Content-Type" :"application/x-www-form-urlencoded"]
let parameters : [String:Any] = ["count":post.likeCount!, "likedby":currentName, "postId=":postItem, "token": post.token!]
Alamofire.request("http://highavenue.co:9000/likesnotification/", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if let data = response.result.value{
print(data)
}
break
case .failure(_):
print(response.result.error as Any)
break
}
}
}
得到这样的控制台错误
2018-07-10 14:21:07.980212+0530 HighAvenue[10584:4236493] Task <B5FC98AB-C3FE-
4D4F-9A93-72D3FFE35DF7>.<1> finished with error - code: -1001
Optional(Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."
UserInfo={NSUnderlyingError=0x1c0e478f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://highavenue.co:9000/likesnotification/, NSErrorFailingURLKey=http://highavenue.co:9000/likesnotification/, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.})
【问题讨论】:
-
你用邮递员之类的其他工具检查过吗……它的反应是什么
-
@santhosh { "status": "success", "code": 200, "error": "" }
-
好的,那么我认为问题在于编码尝试一次,从参数中删除编码或使用 urlencoding.default。所以,它采用默认值
-
@santhosh 怎么做..
-
用 Alamofire.request("highavenue.co:9000/likesnotification", 方法替换 Alamofire.request("highavenue.co:9000/likesnotification", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil) :.post,参数:参数)
标签: ios swift httprequest alamofire