gpt4 book ai didi

json - 更新到 Swift 3 时使用 Alamofire 和 SkyScanner 时出现部分空响应

转载 作者:行者123 更新时间:2023-11-30 12:38:09 24 4
gpt4 key购买 nike

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

}

最佳答案

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

关于json - 更新到 Swift 3 时使用 Alamofire 和 SkyScanner 时出现部分空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42607520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com