简体   繁体   English

iOS:来自 URLSession.shared.dataTask 的数据(带有:url 始终为零(在 xcode 调试器中显示 0 字节错误?)

[英]iOS: Data from URLSession.shared.dataTask(with: url is always nil (display 0 bytes bug in xcode debugger?)

I tried that code found on the web, but data is always nil.我尝试了在网上找到的代码,但数据始终为零。 But when I tap it in safari, I can see a result...但是当我在 safari 中点击它时,我可以看到结果......

 let urlStr =  "https://gmxx2.x.frxx.com:x/xxx/xxx/FR&174"

        let url = URL(string: urlStr)
        URLSession.shared.dataTask(with: url!, completionHandler: {
            (data, response, error) in
            if(error != nil){
                print("error")
            }else{
                do{
                    let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]


                    OperationQueue.main.addOperation({

                    })

                }catch let error as NSError{
                    print(error)
                }
            }
        }).resume()

Here is the response value where it seems not to show problems:这是似乎没有显示问题的响应值:

▿ Optional<NSURLResponse>
  - some : <NSHTTPURLResponse: 0x6000004e14c0> { URL: https://www.burningmeter.com/tournaments.json?page=1 } { Status Code: 200, Headers {
    "Access-Control-Allow-Origin" =     (
        "*");
    "Cache-Control" =     (
        "max-age=0, private, must-revalidate");
    Connection =     (
        "keep-alive");
    "Content-Type" =     (
        "application/json; charset=utf-8");
    Date =     (
        "Fri, 08 Feb 2019 10:46:28 GMT" );
    Etag =     (
        "W/\"c9cf3d615d984a1392782546f941543b\"");
    Server =     (
        Cowboy);
    "Strict-Transport-Security" =     (
        "max-age=31536000");
    "Transfer-Encoding" =     (
        Identity);
    Via =     (
        "1.1 vegur");
    "X-Content-Type-Options" =     (
        nosniff);
    "X-Frame-Options" =     (
        SAMEORIGIN);
    "X-Request-Id" =     (
        "08e98d62-cf30-4028-9cd9-12668f1754ca");
    "X-Runtime" =     (
        "0.021782" );
    "X-Xss-Protection" =     (
        "1; mode=block");} }

The error value is equal to nil.错误值等于 nil。

I precise that I put App Transport Security Settings to YES in info.plist我确切地说我在info.plist中将App Transport Security SettingsYES

EDIT:编辑:
It works with the option to [] but I was looking only for the value of data that was equal to 0 bytes, but the json has a value so it works.它与 [] 选项一起工作,但我只寻找等于 0 字节的数据值,但 json 有一个值,所以它可以工作。
在此处输入图片说明 Even with that param:即使有那个参数:
在此处输入图片说明 EDIT2: But when I try with that WS: https://gmp2.newtelapps.fr:5000/guests/contacts/FR&174 it doesn't work. EDIT2:但是当我尝试使用该 WS 时: https://gmp2.newtelapps.fr:5000/guests/contacts/FR&174 ://gmp2.newtelapps.fr:5000/guests/contacts/FR&174它不起作用。

The only difference I can see is the response format in safari我能看到的唯一区别是 safari 中的响应格式

https://www.burningmeter.com/tournaments.json?page=1 => {...} https://www.burningmeter.com/tournaments.json?page=1 => {...}

https://gmxx2.x.frxx.com:x/xxx/xxx/FR&174 => [{...}] https://gmxx2.x.frxx.com:x/xxx/xxx/FR&174 => [{...}]

Error message: Could not cast value of type '__NSArrayI' (0x108ac6da8) to 'NSDictionary' (0x108ac5818).错误消息: Could not cast value of type '__NSArrayI' (0x108ac6da8) to 'NSDictionary' (0x108ac5818). Thanks in advance.提前致谢。

I made a slight modifications on your code, I have debugged and got the response.我对你的代码做了一些修改,我已经调试并得到了响应。

        let urlString = "https://www.burningmeter.com/tournaments.json?page=1"
        guard let requestUrl = URL(string:urlString) else { return }
        var request = URLRequest(url:requestUrl)
        request.httpMethod = "GET"
        let task = URLSession.shared.dataTask(with: request) {
            (data, response, error) in
            if error == nil, let usableData=data {
                let response =  try? JSONSerialization.jsonObject(with: usableData, options:[])
                print(response) //JSONSerialization
            }
        }
        task.resume()

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

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