1

コードサンプルはXcode 8(β3)で動作するSwift 3.0です。

この Weather Underground URL (MyKey有効なキーに置き換えた場合) は、指定された場所の現在の天気を含む JSON 文字列を返します... http://api.wunderground.com/api/MyKey/conditions/q/51.3276,-1.0022 .json

以下のコードは同じことを行うはずですが、代わりにエラーをスローします (「要求された URL はこのサーバーで見つかりませんでした。」、詳細は下部にあります)。

コードに欠けているものを誰でも見ることができますか?

private let key = "MyKey" // Substitute a valid Weather Underground key here.

@IBAction func fetchWeather() {
    let session = URLSession.shared
    let url     = URL(fileURLWithPath: "http://api.wunderground.com/api/\(key)/conditions/q/51.3276,-1.0022.json")

    let handler = { (data: Data?, response: URLResponse?, error: NSError?) -> Void in
        guard let data = data, let response = response, error == nil else {
            print("\nError: \(error?.description)\n")
            return
        }
        print("\nData: \(data)\n")
        print("\nResponse: \(response)\n")
    }

    let task    = session.dataTask(with: url, completionHandler: handler)
    task.resume()
}

Error: Optional("Error Domain=NSURLErrorDomain Code=-1100 \"The requested URL was not found on this server.\" UserInfo={NSUnderlyingError=0x600000442520 {Error Domain=kCFErrorDomainCFNetwork Code=-1100 \"(null)\"}, NSErrorFailingURLStringKey=file:///http:/api.wunderground.com/api/MyKey/forecast/q/51.3276,-1.0022.json, NSErrorFailingURLKey=file:///http:/api.wunderground.com/api/MyKey/forecast/q/51.3276,-1.0022.json, NSLocalizedDescription=The requested URL was not found on this server.}")

4

0 に答える 0