我如何将 alamofire 用于 post api
How can i use alamofire for post api
我是 iOS 开发新手。我在 swift 3
中使用 alamofire
。我如何在 alamofire
中发送 post 请求。它还在方法中提供了额外的参数。
提前致谢
首先,您将 almofire
库添加到您的项目中,然后将 almofire 导入到您的 ViewController 中,然后将以下方法应用到您的按钮操作中。
func webServiceLogin(isFbLogin:Bool,email:String,password:String)
{
var parameters:[String:String]?
parameters = ["hash":email as String,"key":password ]
Alamofire.request("your url", method: .post, parameters: parameters,encoding: URLEncoding.default, headers: nil).responseJSON {
response in
hideHud(self.view)
switch response.result {
case .success:
if let dictSuccess:NSDictionary = response.value as! NSDictionary?
{
}
break
case .failure(let error):
Alert.showAlertWithTitle(strTitle: appTitle, strMessage: error.localizedDescription, onView: self)
print(response)
print(error)
}
}
}
像下面这样使用并传递您要在服务器中发送的参数。最好使用它编写一个网络层 class 然后它将在整个应用程序中重复使用。
static func serverRequest(urlString: URL?, Parameter:NSDictionary?, completion: @escaping (_ serverResponse: AnyObject?,_ error:NSError?)->()){
// let parameters: Parameters = ["foo": "bar"]
//let headers = ["Authorization": "123456"]
Alamofire.request(urlString!, parameters:nil, headers: nil).responseJSON { response in
if(response.result.value != nil){
let serverResponse = JSON(response.result.value!)
//print("Array value is \(serverResponse.arrayValue)")
completion(serverResponse as AnyObject?, nil)
}
else{
completion(nil, response.result.error as NSError?)
}
}
}
您可以使用 alamofire 管理器
var alamoFireManager = Alamofire.SessionManager
let request = URLRequest(url:_yourULR)
request.HTTPMethod = requestMethod.rawValue
request.timeoutInterval = //set yours
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("gzip", forHTTPHeaderField: "Accept-Encoding")
request.HTTPBody = "you_bodydataSTring".dataUsingEncoding(String.Ecoding.utf8)
alamoFireManager.request(request)
.validate()
.responseString { (response) -> Void in
let datastring = NSString(data:response.data!, encoding: String.Ecoding.utf8)
switch response.result {
case .Success:
if response.response?.statusCode == 200 {
//code for success
}else{
//others
}
case .Failure(let error):
//request failed
}
}
}
我是 iOS 开发新手。我在 swift 3
中使用 alamofire
。我如何在 alamofire
中发送 post 请求。它还在方法中提供了额外的参数。
提前致谢
首先,您将 almofire
库添加到您的项目中,然后将 almofire 导入到您的 ViewController 中,然后将以下方法应用到您的按钮操作中。
func webServiceLogin(isFbLogin:Bool,email:String,password:String)
{
var parameters:[String:String]?
parameters = ["hash":email as String,"key":password ]
Alamofire.request("your url", method: .post, parameters: parameters,encoding: URLEncoding.default, headers: nil).responseJSON {
response in
hideHud(self.view)
switch response.result {
case .success:
if let dictSuccess:NSDictionary = response.value as! NSDictionary?
{
}
break
case .failure(let error):
Alert.showAlertWithTitle(strTitle: appTitle, strMessage: error.localizedDescription, onView: self)
print(response)
print(error)
}
}
}
像下面这样使用并传递您要在服务器中发送的参数。最好使用它编写一个网络层 class 然后它将在整个应用程序中重复使用。
static func serverRequest(urlString: URL?, Parameter:NSDictionary?, completion: @escaping (_ serverResponse: AnyObject?,_ error:NSError?)->()){
// let parameters: Parameters = ["foo": "bar"]
//let headers = ["Authorization": "123456"]
Alamofire.request(urlString!, parameters:nil, headers: nil).responseJSON { response in
if(response.result.value != nil){
let serverResponse = JSON(response.result.value!)
//print("Array value is \(serverResponse.arrayValue)")
completion(serverResponse as AnyObject?, nil)
}
else{
completion(nil, response.result.error as NSError?)
}
}
}
您可以使用 alamofire 管理器
var alamoFireManager = Alamofire.SessionManager
let request = URLRequest(url:_yourULR)
request.HTTPMethod = requestMethod.rawValue
request.timeoutInterval = //set yours
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("gzip", forHTTPHeaderField: "Accept-Encoding")
request.HTTPBody = "you_bodydataSTring".dataUsingEncoding(String.Ecoding.utf8)
alamoFireManager.request(request)
.validate()
.responseString { (response) -> Void in
let datastring = NSString(data:response.data!, encoding: String.Ecoding.utf8)
switch response.result {
case .Success:
if response.response?.statusCode == 200 {
//code for success
}else{
//others
}
case .Failure(let error):
//request failed
}
}
}