我正在使用 Travelpayouts API,不幸的是我得到:
“网络连接丢失”
它曾经工作得很好,我没有改变任何事情,它就开始发生了。我正在使用物理设备。它开始在模拟器和物理设备上显示此错误。有时它可能会起作用,但现在 95% 的时间都不起作用。
func getFlightsRoot(destinationAirports: [Airport], departureAirports: [Airport], destinationAirport: Airport, departureAirport: Airport, departureDate: String, arrivalDate: String, adultsCount: Int, IPAddress: String, budget: Int, isOneWay: Bool, iteration: Int = 0) {
self.destinationAirports = destinationAirports
self.departureAirports = departureAirports
var flightClass = ""
if (budget == 1 || budget == 2 || budget == 4) {
flightClass = "Y"
} else {
flightClass = "C"
}
var signatureTemp = ""
var signature = ""
var segments: [FlightSegment] = []
if isOneWay {
signatureTemp = MD5("KEY):beta.aviasales.com:en-us:\(Marker):\(adultsCount):0:0:\(departureDate):\(destinationAirport.iata!):\(departureAirport.iata!):\(flightClass):\(IPAddress)")
signature = signatureTemp.lowercased()
segments = [
FlightSegment(origin: departureAirport.iata!, destination: destinationAirport.iata!, date: departureDate)
]
} else {
signatureTemp = MD5("\(KEY):beta.aviasales.com:en-us:\(Marker):\(adultsCount):0:0:\(departureDate):\(destinationAirport.iata!):\(departureAirport.iata!):\(arrivalDate):\(departureAirport.iata!):\(destinationAirport.iata!):\(flightClass):\(IPAddress)")
signature = signatureTemp.lowercased()
segments = [
FlightSegment(origin: departureAirport.iata!, destination: destinationAirport.iata!, date: departureDate),
FlightSegment(origin: destinationAirport.iata!, destination: departureAirport.iata!, date: arrivalDate)
]
}
let request = FlightRequest(
signature: signature,
marker: "\(Marker)",
host: "beta.aviasales.com",
userIP: IPAddress,
locale: "en-us",
tripClass: flightClass,
passengers: Passengers(adults: adultsCount, children: 0, infants: 0),
segments: segments
)
let urlString = "https://api.travelpayouts.com/v1/flight_search"
guard let url = URL(string: urlString) else {
print("Invalid URL")
return
}
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
// Encode request body
let encoder = JSONEncoder()
do {
let jsonData = try encoder.encode(request)
urlRequest.httpBody = jsonData
} catch {
print("Error encoding request: \(error)")
return
}
// Set headers
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: urlRequest) { [weak self] data, response, error in
guard let data = data, error == nil else {
self?.retryGetFlightsRoot(destinationDate: departureDate, arrivalDate: arrivalDate, adultsCount: adultsCount, IPAddress: IPAddress, budget: budget, isOneWay: isOneWay, iteration: iteration)
print("ERROR in Flights API 1: \(error?.localizedDescription)")
return
}
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
self?.retryGetFlightsRoot(destinationDate: departureDate, arrivalDate: arrivalDate, adultsCount: adultsCount, IPAddress: IPAddress, budget: budget, isOneWay: isOneWay, iteration: iteration)
print("Error in Flights API 2: \(response as? HTTPURLResponse)")
return
}
do {
let decoder = JSONDecoder()
let responseData = try decoder.decode(FlightRootResponse.self, from: data)
self?.currencyRates = responseData.currency_rates
self?.searchID = responseData.search_id
self?.getFlights(searchID: responseData.search_id, iteration: iteration, destinationDate: departureDate, arrivalDate: arrivalDate, adultsCount: adultsCount, IPAddress: IPAddress, budget: budget, isOneWay: isOneWay)
} catch {
self?.retryGetFlightsRoot(destinationDate: departureDate, arrivalDate: arrivalDate, adultsCount: adultsCount, IPAddress: IPAddress, budget: budget, isOneWay: isOneWay, iteration: iteration)
}
}
task.resume()
}
航班 API 1 中的错误:可选(“网络连接丢失。”)
我要向您指出的主要资源在这里:
https://developer.apple.com/library/archive/qa/qa1941/_index.html#//apple_ref/doc/uid/DTS40017602
您可能需要查看服务器端日志,因为如果这种情况频繁发生,则连接可能在服务器端关闭,而不是由网络错误引起。 那个人或某人启用了低数据模式。关闭您的应用程序的蜂窝网络访问,或关闭数据漫游。
另一种可能性是,您上传的数据本身随着时间的推移而发生了变化,并且已经变得如此之大,以至于您很可能在缓慢的蜂窝链路上上传超时。
如果这些都不适用,那么您真正能做的唯一事情就是使用网络分析器捕获数据包跟踪,看看您是否可以弄清楚发生了什么,可以选择插入诸如 Charles Proxy 内联之类的东西来捕获更多数据,然后将其归档针对 iOS 的一个错误,假设它不是服务器端回归。