ios - 使用 Rest API Swift 3

标签 ios iphone swift swift3

我在使用 SWIFT 3 时遇到一个问题。当我使用具有两个不同 URL 的 rest api 时,出于某种原因,此 URL“http://sistemas2.laprensa.com.ni/LaPrensa/api/Beacons”无法正常工作,而此 URL“https://app.ccn.com.ni:8080/BeerWayNicaragua/api/BusinessType”正在工作。我检查了 JSON,没问题。请帮助我了解问题所在。

没有工作

    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()

最佳答案

尝试发出请求时阅读控制台日志。你会注意到提示。

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

解决方法如下

继续将以下键添加到您的 info.plist 文件中 vis source

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

enter image description here

来自 plist 编辑器。

关于ios - 使用 Rest API Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45452445/

相关文章:

ios - NSDateComponent 给我一天递增 1

ios - 如何在 secondVC 中发生更改时自动实时更新 firstVC 中的 UITableViewCell

objective-c - iOS 无法识别的选择器发送到实例 - 添加 NSDictionary 到 NSMutableArray

swift - UIBarButtonItem 选择/事件时为粗体非标准字体

ios - Swift - Firebase 如何在没有父项的情况下仅获取子项值

ios - ImageView 加载本地镜像而不是从 URL

iphone - 自定义类上的sortedArrayUsingSelector?

ios - Swift tableView.reloadRows 删除单元格

ios - 将 AVPlayerLayer 添加为当前 View 的子层在 iPhone 5 上不起作用

ios - 更新到 8.3 Xcode Swift 3.1 后输入不是 aPCH 文件(swift 编译器错误)