東京メトロ10周年スペシャルサイトから開発者登録します。
http://tokyometro10th.jp/future/opendata/index.html
アカウント>アクセストークンの確認>追加からアクセストークンの確認・追加が出来ます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// metroExit | |
// | |
// Created by takashi on 10/18/14. | |
// Copyright (c) 2014 takashi otaki. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController{ | |
//運行状況を格納する変数を宣言 | |
var metroTrainInformation: NSArray = [] | |
var trainInformationText: [String] = [] | |
var trainInformationRailway: [String] = [] | |
//アクセストークンを宣言 | |
let consumerKey = "ACL_CONSUMERKEY(取得したアクセストークン)" | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
//運行状況の取得 | |
getDataTrainInfomation() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
//全線の運行状況の取得 | |
func getDataTrainInfomation() { | |
var URL = NSURL(string: "https://api.tokyometroapp.jp/api/v2/datapoints?rdf:type=odpt:TrainInformation&acl:consumerKey=\(consumerKey)") | |
var req = NSURLRequest(URL: URL!) | |
// NSURLConnectionを使ってAPIを取得する(非同期) | |
NSURLConnection.sendAsynchronousRequest(req, | |
queue: NSOperationQueue.mainQueue(), | |
completionHandler: response) | |
} | |
//★★★★★★★★★★★★★★★★★★ | |
// 取得したAPIデータの処理 | |
func response(res: NSURLResponse!, data: NSData?, error: NSError!){ | |
var json:NSArray = NSJSONSerialization.JSONObjectWithData(data!, | |
options: NSJSONReadingOptions.AllowFragments, error: NSErrorPointer()) as NSArray | |
metroTrainInformation = json | |
//全線の運行状況の情報をコンソールに表示 | |
println(metroTrainInformation) | |
} | |
} |
0 コメント:
コメントを投稿