在更新为Swift 3时使用Alamofire和Skyscanner时,部分空的响应



我正在将应用程序更新为Swift 3,并在Alamofire和.get请求中遇到一些麻烦。使用下面的代码,我正在提出一个.get请求,以从Skyscanner的API获取飞行数据。无论出于何种原因,最终的返回json值始终返回部分为空。我收到了一条成功的消息,即更新已经完成,并且其中几个元素包含正确的数据,但是其他元素(例如行程信息和运营商信息)始终是空的。

我尝试更改响应类型(JSON,String,Data等),但这似乎并不能解决问题。该代码在Swift 2中运行良好,仅在最近的更新中,一切都破裂了。关于什么可能导致部分空的JSON响应?

import UIKit
import SwiftyJSON
import Alamofire
class ViewController: UIViewController {
var key = "prtl6749387986743898559646983194"
override func viewDidLoad() {
    super.viewDidLoad()
    getFlights()
}
func getSessionKey(destination: String, outboundDate: String, inboundDate: String, adults: Int, complete: @escaping(_ key: String?, _ error: NSError?) -> Void) {
    // Use the mandatory headers listed on the SkyScanner api page http://business.skyscanner.net/portal/en-GB/Documentation/FlightsLivePricingList
    let headers: HTTPHeaders = [
        "Content-Type":"application/x-www-form-urlencoded",
        "Accept":"application/json"
    ]
    let parameters: Parameters = [
        "apiKey":key,
        "country":"US",
        "currency":"USD",
        "locationSchema":"iata",
        "locale":"EN",
        "originplace":"DUB",
        "destinationplace":"LON",
        "outbounddate":"2017-03-12",
        "inbounddate":"2017-03-20",
        "adults":1,
        "groupPricing":true
    ]
    // First get the session URL key so we can being the poll
    let sessionURL = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0"
    let sessionPost = Alamofire.request(sessionURL, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers)
    sessionPost.responseString { (sessionResponse) in
        switch sessionResponse.result {
        case .success:
            if let responseHeader = sessionResponse.response?.allHeaderFields {
                // Get the session poll URL from the location header
                let locationHeader = responseHeader["Location"] as? String
                if let range = locationHeader?.range(of: "v1.0/") {
                    let sessionKey = locationHeader?.substring(from: range.upperBound)
                    complete(sessionKey, nil)
                }
            }
        case .failure(let error):
            complete(nil, error as NSError?)
        }
    }
}
func poll(sessionKey: String, complete: @escaping(_ data: JSON?, _ error: NSError?) -> Void) {
    // Take the new found session key and plug it into the poll func
    let pollingParameters: Parameters = [
        "sortype":"price",
        "sortorder":"asc",
        "includeQuery": false,
        "stops": 2,
        "includeCurrencyLookup": false,
        "includeBookingDetailsLink": true
    ]
    let headers: HTTPHeaders = [
        "Content-Type":"application/x-www-form-urlencoded",
        "Accept":"application/json"
    ]
    // **PROBLEM HERE AS THE RETURNED VALUE ALWAYS CONTAINS SOME EMPTY ELEMENTS FOR AN UNDETERMINED REASON**
    let pollURL = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/(sessionKey)?apiKey=(key)"
    let sessionPoll = Alamofire.request(pollURL, method: .get, parameters: pollingParameters, encoding: URLEncoding.default, headers: headers)
    sessionPoll.responseJSON(queue: DispatchQueue.global() ,completionHandler: { (response) in
        print(response.response?.statusCode ?? "There is no response")
        switch response.result {
        case .success:
            if let value = response.result.value {
                //print("RawValue: (value)")
                complete(JSON(value), nil)
            }
        case .failure(let error):
            complete(nil, error as NSError?)
        }
    })
}
func beginPolling(sessionKey: String, complete: @escaping (_ itineraries: [String:[[String:JSON]]]?, _ error: NSError?) -> Void) {
    self.poll(sessionKey: sessionKey) { (data, error) in
        if error == nil {
            if data?["Status"].stringValue == "UpdatesPending"{
                let when = DispatchTime.now() + 1
                DispatchQueue.global().asyncAfter(deadline: when, execute: {
                    print("Updates Pending")
                    self.beginPolling(sessionKey: sessionKey, complete: { (trips, error) in
                        complete(trips, error)
                    })
                })
            }else if data?["Status"].stringValue == "UpdatesComplete" {
                print("Updates Complete: (data)")
            }else {
                // There is no Status and we've probably errored out somewhere
            }
        }else{
            // Error
        }
    }
}
func getFlights() {
    getSessionKey(destination: "LON", outboundDate: "2017-03-12", inboundDate: "2017-03-20", adults: 1) { (sessionKey, error) in
        if error == nil{
            let when = DispatchTime.now() + 1
            DispatchQueue.global().asyncAfter(deadline: when, execute: {
                self.beginPolling(sessionKey: sessionKey!, complete: { (trips, error) in
                    if error == nil {
                        // Take the valid data and pass it on to the next viewcontroller
                    }else{
                        // Error
                    }
                })
            })
        }else {
            print("There has been an Error getting the session key")
        }
    }
}

}

API文档在没有任何人注意到的情况下更改。"停止"参数仅接受0或1的值,并且任何其他输入都会导致我们一直看到的奇怪输出。

最新更新