簡體   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?)

我嘗試了在網上找到的代碼,但數據始終為零。 但是當我在 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()

這是似乎沒有顯示問題的響應值:

▿ 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");} }

錯誤值等於 nil。

我確切地說我在info.plist中將App Transport Security SettingsYES

編輯:
它與 [] 選項一起工作,但我只尋找等於 0 字節的數據值,但 json 有一個值,所以它可以工作。
在此處輸入圖片說明 即使有那個參數:
在此處輸入圖片說明 EDIT2:但是當我嘗試使用該 WS 時: https://gmp2.newtelapps.fr:5000/guests/contacts/FR&174 ://gmp2.newtelapps.fr:5000/guests/contacts/FR&174它不起作用。

我能看到的唯一區別是 safari 中的響應格式

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

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

錯誤消息: Could not cast value of type '__NSArrayI' (0x108ac6da8) to 'NSDictionary' (0x108ac5818). 提前致謝。

我對你的代碼做了一些修改,我已經調試並得到了響應。

        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