更新到Swift时使用Alamofire和SkyScanner时的部分空响应3

时间:2017-03-05 10:59:26

标签: json swift alamofire skyscanner

我正在将应用更新到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")
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

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