找不到请求的网址

时间:2016-07-28 08:11:58

标签: json swift xcode swift3 xcode8-beta3

代码示例是在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.}")

0 个答案:

没有答案