0

我正在将一个应用程序更新到 Swift 3,并且在使用 Alamofire 和 .get 请求时遇到了一些问题。使用下面的代码,我发出 .get 请求以从 SkyScanner 的 api 获取航班数据。无论出于何种原因,最终 .get 请求的返回 JSON 值总是返回部分空的。我收到一条更新已完成的成功消息,其中一些元素包含正确的数据,但其他元素(如行程信息和承运人信息)始终为空。

我曾尝试更改响应类型(JSON、字符串、数据等),但这似乎并不能解决问题。这段代码在 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")
        }
    }
}

}

4

1 回答 1

0

api 文档在没有人注意到的情况下发生了变化。“stops”参数只接受 0 或 1 的值,任何其他输入都会导致我们看到的奇怪输出。

于 2017-03-06T11:10:28.270 回答