【问题标题】:Alamofire post method in iOS Swift 4?iOS Swift 4 中的 Alamofire 发布方法?
【发布时间】: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


【解决方案1】:

那是因为你在网络调用中没有设置请求时间,默认你的请求时间间隔很小,所以请增加请求超时时间。像这样的,

let request = NSMutableURLRequest(url: URL(string: "")!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.timeoutInterval = 120 // 120 secs
let values = ["key": "value"]
request.httpBody = try! JSONSerialization.data(withJSONObject: values, options: [])
Alamofire.request(request as! URLRequestConvertible).responseJSON {
    response in
    // do whatever you want here
}

您的代码中的第二个错误是您尝试访问默认情况下不允许的 http url,因此您必须从您的应用程序中绕过此安全性,请请参阅此答案以从您的应用程序中删除此安全层。 The resource could not be loaded because the App Transport Security policy requires the use of a secure connection

【讨论】:

  • 感谢您的回答。它崩溃了。无法将“NSMutableURLRequest”(0x1036f8d10)类型的值转换为“Alamofire.URLRequestConvertible”(0x105a851b8)。 2018-07-10 14:52:43.253861+0530 HighAvenue[10598:4246892] 无法将“NSMutableURLRequest”(0x1036f8d10)类型的值转换为“Alamofire.URLRequestConvertible”(0x105a851b8)。
  • 你可能正在尝试访问Almofire.request,这是不同的,请像这样创建一个请求对象,然后将其传递给Almofire的函数
  • 对不起!我找不到你。
  • 不,问题,只需将您的代码与我提供给您的代码块进行比较即可。您在一行中传递参数,我正在创建一个请求对象,然后将其传递给函数。我要说的是清理你的代码并制作这个请求对象并将时间间隔设置为 120 秒。
  • 你可以检查这个答案的时间间隔...stackoverflow.com/questions/36626075/…
猜你喜欢
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 1970-01-01
  • 2017-05-11
  • 2019-04-13
  • 2018-10-10
  • 1970-01-01
相关资源
最近更新 更多