美文网首页
使用Alamofire发送GET和POST请求

使用Alamofire发送GET和POST请求

作者: 我非起点亦非终点 | 来源:发表于2016-08-29 17:32 被阅读0次

Alamofire是swift下使用最多的第三方网络请求库,看一下用Alamofire怎么发送网络请求
环境:Xcode7.2 swift2.1.1 Alamofire2.0
首先用CocoaPods下载Alamofire

target "LKQSwift" do
platform :ios,'8.0'
use_frameworks!
pod 'Alamofire','~>2.0'
pod 'SDWebImage','~>3.7'
end

在需要用到网络请求的类 import Alamofire

POST请求:

let url = "http://xxxxxxxxxxxxx"
let dic = ["act":"homepage","act_2":"index","r_type":"1"]
Alamofire.request(.POST, url, parameters: dic, encoding: .URL, headers: nil).validate().responseJSON { (request, response, result) -> Void in
     let jsonDic:NSDictionary = result.value as! NSDictionary
        self.array = jsonDic.objectForKey("list") as! NSArray
        self.tableView.reloadData()
}

GET请求

  let url = "http://xxxxxxxxxxxxx?act=homepage&act_2=index&r_type=1"
  Alamofire.request(.GET, url).validate().responseJSON(completionHandler: { (request, response, result) -> Void in
        print(result.value)
  })

GitHub:AlamofireTest

相关文章

网友评论

      本文标题:使用Alamofire发送GET和POST请求

      本文链接:https://www.haomeiwen.com/subject/ipaxettx.html