我尝试了在网络上找到的代码,但数据始终为零。但是当我在野生动物园中点击它时,我可以看到结果...
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");} }
错误值等于零。
我精确地将App Transport Security Settings
放在YES
的{{1}}
编辑:
它可以与[]选项一起使用,但我只在寻找等于0个字节的数据值,但是json具有一个值,因此可以正常工作。
即使有该参数:
EDIT2:
但是,当我尝试使用该WS:info.plist
时,它将无法正常工作。
我唯一看到的区别是野生动物园中的响应格式
https://gmp2.newtelapps.fr:5000/guests/contacts/FR&174
=> {...}
https://www.burningmeter.com/tournaments.json?page=1
=> [{...}]
错误消息:https://gmxx2.x.frxx.com:x/xxx/xxx/FR&174
预先感谢。
答案 0 :(得分:0)
我对您的代码做了一些修改,我已经调试并得到了响应。
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()