Alamofire纯Swift网路库使用

Alamofire是一个好用的Swift写的网路库,相对URLSession来说,使用方便很多

简单的GET请求

1
2
3
4
5
6
7
8
let parameters: [String: AnyObject] = ["access_token": accessToken]
let weiboTimelineURL = "https://api.weibo.com/2/statuses/home_timeline.json"
Alamofire.request(.GET, weiboTimelineURL,
parameters: parameters,
encoding: URLEncoding.queryString)
.responseJSON { response in
debugPrint(response)
}

POST

1
2
3
4
5
6
let body: [String: AnyObject] = ["access_token": accessToken]
let weiboCommentsURL = "https://api.weibo.com/2/statuses/home_timeline.json"
Alamofire.request(.POST, weiboCommentsURL, parameters: body, encoding: URLEncoding.httpBody)
.responseJSON { response in
debugPrint(response)
}

其他

具体文档:https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md