Swift Json - 从 API 获取数据
Swift Json - get data from API
我在尝试从 Web api 获取数据时遇到问题。但我得到的是零。
我无法从 api 获取文章列表。有什么建议吗?
api 文档地址:yazar.io/doc/
我的 NSObject 文件:
import Alamofire
struct YazarIoService {
// Doc http://yazar.io/doc/
private static let baseURL = "http://yazar.io"
private enum ResourcePath: Printable {
case Articles
case StoryUpvote(storyId: Int)
var description: String {
switch self {
case .Articles: return "/api/article/feed/1"
case .StoryUpvote(let id): return "/api/client/favorites/action/"
}
}
}
static func articleForSection(section: String, page: Int, response: (JSON) -> ()){
let urlString = baseURL + ResourcePath.Articles.description
println(urlString)
let parameters = [
"page": toString(page)
]
println(parameters)
Alamofire.request(.GET, baseURL, parameters: parameters).responseJSON{(_, _, data, _) -> Void in
let articles = JSON(data ?? [])
response(articles)
}
}
我的 table 视图控制器;
class articlesTableViewController: UITableViewController, MakaleTableViewCellDelegate {
let transitionManager = TransitionManager()
var articles: JSON! = []
func loadarticles(section: String, page: Int){
YazarIoService.articleForSection(section, page: page) { (JSON) -> () in
self.articles = JSON["articles"]
self.tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
loadarticles("", page: 1)
println(articles[])
}
@IBAction func MenuButtonDidTouch(sender: AnyObject) {
performSegueWithIdentifier("MenuSegue", sender: self)
}
@IBAction func LoginbutonDidTouch(sender: AnyObject) {
performSegueWithIdentifier("LoginSegue", sender: self)
}
//number of rows
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return articles.count //
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MakaleCell") as MakaleTableViewCell //--> cell'in özelliklerine erişmek içim bunu yazdık. Eskiden UITableViewCell'di. Değiştirmezsen cell.title... vs çalışmakz.
let makale = articles[indexPath.row]
cell.configureWithMakale(makale)
cell.delegate = self
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("WebSegue", sender: indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// MARK: MakaleTableViewCellDelegate
func makaleTableViewCellDidTouchUpvote(cell: MakaleTableViewCell, sender: AnyObject) {
// TODO: Implement Upvote
}
func makaleTableViewCellDidTouchComment(cell: MakaleTableViewCell, sender: AnyObject) {
performSegueWithIdentifier("CommentsSegue", sender: self)
}
// MARK: Misc
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "WebSegue" {
let toView = segue.destinationViewController as WebViewController
let indexPath = sender as NSIndexPath
let url = articles[indexPath.row]["article_url"].string!
toView.url = url
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Fade)
toView.transitioningDelegate = transitionManager
}
}
}
您正在请求中使用 baseURL
static func articleForSection(section: String, page: Int, response: (JSON) -> ()){
let urlString = baseURL + ResourcePath.Articles.description
println(urlString)
let parameters = [
"page": toString(page)
]
println(parameters)
Alamofire.request(.GET, baseURL, parameters: parameters).responseJSON{(_, _, data, _) -> Void in
let articles = JSON(data ?? [])
response(articles)
}
}
像这样将 baseURL 更改为 urlString
Alamofire.request(.GET, urlString, parameters: parameters).responseJSON{(_, _, data, _) -> Void in
let articles = JSON(data ?? [])
response(articles)
}
这个 link http://yazar.io/api/newspaper/1 给出了一个 404 错误,所以很明显你会从请求中返回一个 nil。
我在尝试从 Web api 获取数据时遇到问题。但我得到的是零。 我无法从 api 获取文章列表。有什么建议吗?
api 文档地址:yazar.io/doc/
我的 NSObject 文件:
import Alamofire
struct YazarIoService {
// Doc http://yazar.io/doc/
private static let baseURL = "http://yazar.io"
private enum ResourcePath: Printable {
case Articles
case StoryUpvote(storyId: Int)
var description: String {
switch self {
case .Articles: return "/api/article/feed/1"
case .StoryUpvote(let id): return "/api/client/favorites/action/"
}
}
}
static func articleForSection(section: String, page: Int, response: (JSON) -> ()){
let urlString = baseURL + ResourcePath.Articles.description
println(urlString)
let parameters = [
"page": toString(page)
]
println(parameters)
Alamofire.request(.GET, baseURL, parameters: parameters).responseJSON{(_, _, data, _) -> Void in
let articles = JSON(data ?? [])
response(articles)
}
}
我的 table 视图控制器;
class articlesTableViewController: UITableViewController, MakaleTableViewCellDelegate {
let transitionManager = TransitionManager()
var articles: JSON! = []
func loadarticles(section: String, page: Int){
YazarIoService.articleForSection(section, page: page) { (JSON) -> () in
self.articles = JSON["articles"]
self.tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
loadarticles("", page: 1)
println(articles[])
}
@IBAction func MenuButtonDidTouch(sender: AnyObject) {
performSegueWithIdentifier("MenuSegue", sender: self)
}
@IBAction func LoginbutonDidTouch(sender: AnyObject) {
performSegueWithIdentifier("LoginSegue", sender: self)
}
//number of rows
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return articles.count //
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MakaleCell") as MakaleTableViewCell //--> cell'in özelliklerine erişmek içim bunu yazdık. Eskiden UITableViewCell'di. Değiştirmezsen cell.title... vs çalışmakz.
let makale = articles[indexPath.row]
cell.configureWithMakale(makale)
cell.delegate = self
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("WebSegue", sender: indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// MARK: MakaleTableViewCellDelegate
func makaleTableViewCellDidTouchUpvote(cell: MakaleTableViewCell, sender: AnyObject) {
// TODO: Implement Upvote
}
func makaleTableViewCellDidTouchComment(cell: MakaleTableViewCell, sender: AnyObject) {
performSegueWithIdentifier("CommentsSegue", sender: self)
}
// MARK: Misc
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "WebSegue" {
let toView = segue.destinationViewController as WebViewController
let indexPath = sender as NSIndexPath
let url = articles[indexPath.row]["article_url"].string!
toView.url = url
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Fade)
toView.transitioningDelegate = transitionManager
}
}
}
您正在请求中使用 baseURL
static func articleForSection(section: String, page: Int, response: (JSON) -> ()){
let urlString = baseURL + ResourcePath.Articles.description
println(urlString)
let parameters = [
"page": toString(page)
]
println(parameters)
Alamofire.request(.GET, baseURL, parameters: parameters).responseJSON{(_, _, data, _) -> Void in
let articles = JSON(data ?? [])
response(articles)
}
}
像这样将 baseURL 更改为 urlString
Alamofire.request(.GET, urlString, parameters: parameters).responseJSON{(_, _, data, _) -> Void in
let articles = JSON(data ?? [])
response(articles)
}
这个 link http://yazar.io/api/newspaper/1 给出了一个 404 错误,所以很明显你会从请求中返回一个 nil。