繁体   English   中英

NSURLErrorDomain 错误代码 1002 说明

[英]NSURLErrorDomain error code 1002 description

我是iOS开发的新手。 我正在尝试加载JSON ,这是我的功能:

func loadmyJSON (urlPath: String) {      

    let url: NSURL = NSURL(string: urlPath)!

    let session = NSURLSession.sharedSession()

    let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
               println("Task completed")
        if(error != nil) {
            // If there is an error in the web request, print it to the console
            println("error not nil::::::")
            println(error.localizedDescription)
        }
        var err: NSError?
        
        var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
        if(err != nil) {
            // If there is an error parsing JSON, print it to the console
            println("JSON Error \(err!.localizedDescription)")
    }
    
        
        let results: NSArray = jsonResult["variants"] as NSArray
                    dispatch_async(dispatch_get_main_queue(), {
            
            self.jsonTable = results
            self.productView!.reloadData()
           })
     })

但我收到此错误:

error not nil::::::

The operation couldn't be completed. (NSURLErrorDomain error -1002.)

我能够通过相同的 URL 在浏览器中获取 JSON。 我曾尝试阅读此 Apple Doc ,但无法获得说明。

这个错误代码是什么意思?

NSURLErrorDomain error -1002表示NSURLErrorUnsupportedURLkCFURLErrorUnsupportedURL 这表明提供了错误的 URL,在大多数情况下,URL 前缀中缺少http://

可以在这里找到所有NSURLErrorDomain代码的列表: https : //developer.apple.com/documentation/foundation/1508628-url_loading_system_error_codes

除了上述答案之外,可能发生这种情况的原因之一是,

您可能在 Info.plist 中将Allow Arbitrary Loads设置为false ,并且您尝试加载的 URL 不在安全服务器上。

对此的解决方案是将其设置为true ,或者转移到安全连接,即https://

希望它可以帮助某人。 祝一切顺利。

我遇到了同样的问题,发现 URL 中缺少关键参数。 您需要为用于发出请求的 URL 中的关键参数设置Google 地图键值 如果您缺少其他键,就会发生这种情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM