在使用swift(3.0+)的Alamofire4.4时需要设置下超时时间,google到的答案都已经无法使用了,估计是答者使用版本太旧,我记录下我使用的方法吧。
var request = URLRequest(url: NSURL.init(string: (your url)) as! URL)
request.httpMethod = "POST"
//超时时间设置
request.timeoutInterval = 10
let postString = "参数1=(参数)&参数2=(参数)"
request.httpBody = postString.data(using: .utf8)
Alamofire.request(request).responseJSON { response in
print(response)
if let JSON = response.result.value {
print("JSON: (JSON)")
}
}
当然你可以改成get方法。
如果你的参数里面有中文,就不要使用字符串传参数了(会出现乱码,和字符串的编码格式无关),应该使用dictionary来传递参数
let par = ["参数":"可以含有中文的参数"]
let url = "你的URL"
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 120
manager.request(url,method:.post,parameters:par).responseJSON{ response in
print(response)
if let JSON = response.result.value {
print("JSON: (JSON)")
}
}