私はSWIFT 3に1つの問題があります。何か別のURLで残りのAPIを消費すると、このURL「http://sistemas2.laprensa.com.ni/LaPrensa/api/Beacons」が動作していないため、このURL「https://app.ccn.com.ni:8080/BeerWayNicaragua/api/BusinessType」が機能しています。私はJSONをチェックしてOKです。問題が何であるかを教えてください。残りのAPI Swift 3を消費する
ませワーキングlet url = "http://sistemas2.laprensa.com.ni/LaPrensa/api/Beacons"
let requestURL: URL = URL(string: url)!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL)
urlRequest.httpMethod = "GET"
urlRequest.addValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
urlRequest.addValue("application/json; charset=UTF-8", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: urlRequest as URLRequest) {data,response,error in
if response != nil {
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
do {
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments)
callback((json as? [[String: AnyObject]])!)
}catch {
print("Error with Json: \(error)")
}
}
}
}
task.resume()
そして、これが動作している、唯一の私は、URLを変更します。私は何が起こったのかわかりません。
let url = "https://app.ccn.com.ni:8080/BeerWayNicaragua/api/BusinessType"
let requestURL: URL = URL(string: url)!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL)
urlRequest.httpMethod = "GET"
urlRequest.addValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
urlRequest.addValue("application/json; charset=UTF-8", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: urlRequest as URLRequest) {data,response,error in
if response != nil {
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
do {
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments)
callback((json as? [[String: AnyObject]])!)
}catch {
print("Error with Json: \(error)")
}
}
}
}
task.resume()
これはhttpとhttpsが原因である可能性があります。 info.plistにNSAppTransportSecurityキーを追加しましたか? –
動作していないと言うと、statusCode == 200を取得していないか、APIを解析できない、またはできないことがあります。 info.plistにNSAppTransportSecurityキーを追加しましたか? –
関連しませんが、GETリクエストを送信するときには 'URLRequest'はまったく必要ありません。 URLを渡すだけです。 Swiftでは 'NSMutableURLRequest'を使わないでください。ネイティブ構造体 'URLRequest'があります。 – vadian