Ios 为什么不从一个URL获得响应?
我正在使用Ios 为什么不从一个URL获得响应?,ios,swift,nsjsonserialization,Ios,Swift,Nsjsonserialization,我正在使用URLSession。我没有收到任何错误或回复 它使用一个url。但是,它彼此不起作用 我还尝试了percentencoding。但它也不起作用 代码如下 let urlString = "https://stark-spire-93433.herokuapp.com/json"//This is not working //let urlString = "https://jsonplaceholder.typicode.com/todos"//This is working let
URLSession
。我没有收到任何错误或回复
它使用一个url。但是,它彼此不起作用
我还尝试了percentencoding
。但它也不起作用
代码如下
let urlString = "https://stark-spire-93433.herokuapp.com/json"//This is not working
//let urlString = "https://jsonplaceholder.typicode.com/todos"//This is working
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)//URLSession.shared
var request = URLRequest(url: URL(string:urlString)!)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: [], options: [])
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
print("response---",response)
print("error--",error)
if data != nil {
let json = try? JSONSerialization.jsonObject(with: data!)
print("JSOn",json)
} else {
print("error data is nil")
}
})
task.resume()
太麻烦的代码
这就足够了
let url = URL(string:"https://stark-spire-93433.herokuapp.com/json")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { print(error!); return }
do {
let json = try JSONSerialization.jsonObject(with: data)
print("JSOn",json)
} catch {
print(error)
}
}
task.resume()
太麻烦的代码
这就足够了
let url = URL(string:"https://stark-spire-93433.herokuapp.com/json")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { print(error!); return }
do {
let json = try JSONSerialization.jsonObject(with: data)
print("JSOn",json)
} catch {
print(error)
}
}
task.resume()
它不是一个urljson@wings有什么建议吗?我正在寻找将json输入我的应用程序。为什么要设置httpBody?@Larme updated!你的代码对我来说很好用。这可能是你的网络问题,而不是urljson@wings有什么建议吗?我正在寻找将json输入我的应用程序。为什么要设置httpBody?@Larme updated!你的代码对我来说很好用。这可能是你这边的网络问题。