首页 > 代码库 > Alamofire的get请求,post请求,以及上传字典
Alamofire的get请求,post请求,以及上传字典
get请求:
Alamofire.request("你的网址",method: .get, parameters: nil, encoding: URLEncoding.default)
.responseJSON { response in
//print("url:\(response.request)") // original URL request
//print("request:\(response.response)") // URL response
//print("data:\(response.data)") // server data
//print("result:\(response.result)") // result of response serializatio
}
let currRequest = Alamofire.request("你的网址", method: .post, parameters: nil, encoding:URLEncoding.default, headers: nil)
currRequest.responseJSON{(responseJson) -> Void in
switch responseJson.result {
case .success(let value):
let swiftyJsonVar = JSON(value)
//将获取的所有的title放到数组里面
// let str = swiftyJsonVar["data"].arrayValue.map({$0["title"].stringValue})
self.resultArr = swiftyJsonVar["data"]
// for index in 1...self.resultArr.count{
// if let str = self.resultArr[index-1]["title"].string {
//
// print(str)
// }
// }
case .failure( _):
// print(error)
ZHBPopTipView.showText("网络错误", position: .bottom)
}
self.tableView.reloadData()
}
post接口并上传字典:
do {
dic = ["key":"value"]
print(mobileDic)
Alamofire.request("你的网址", method: .post, parameters:dic, encoding: URLEncoding.default, headers:nil).responseJSON(completionHandler: { (response)in
switch response.result{
case .success:
iflet dic:NSDictionary = (response.result.value as? NSDictionary){
print(dic)
}
case .failure(let error):
print(error)
}
})
} catch {
print(error)
}
Alamofire的get请求,post请求,以及上传字典