gpt4 book ai didi

ios - postman 响应正常,但在 Swift 3 上的 Alamofire 返回失败

转载 作者:行者123 更新时间:2023-11-28 19:29:56 26 4
gpt4 key购买 nike

我使用 GoogleMap Direction API 绘制多个位置的路线。我用 postman 检查响应状态,我可以检查 200 OK。但问题是当我使用 Alamofire 时,响应是失败!

当我从起点画到终点时,响应正常。(总是)但是如果我添加额外的位置,Alamofire 返回失败。

有人遇到同样的问题并解决了吗?

enter image description here

enter image description here

下面是我的代码。谢谢!

 originAddress = locationInfoList[0]
destinationAddress = locationInfoList.last
var wayPointUrl:String = ""
var waypoint = locationInfoList.dropFirst()
waypoint = waypoint.dropLast()

for coordinate in waypoint {
print("~~~~")

let wayPoint = "|\(coordinate.latitude),\(coordinate.longitude)"
wayPointUrl.append(wayPoint)
print("~XXXXXXX~~~")
print(wayPointUrl)
print("XXXXXXX")
}

directionURL = "https://maps.googleapis.com/maps/api/directions/json?" +
"origin=\(originAddress.latitude),\(originAddress.longitude)&destination=\(destinationAddress.latitude),\(destinationAddress.longitude)&waypoints=optimize:true\(wayPointUrl)" +
"&key=apikey"

print("VVVVVVV")
print(directionURL)
print("VVVVVVVV")
}

Alamofire.request(directionURL, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in

print(response)

if let JSON = response.result.value {

print(JSON)

let mapResponse: [String: AnyObject] = JSON as! [String : AnyObject]

let routesArray = (mapResponse["routes"] as? Array) ?? []

let routes = (routesArray.first as? Dictionary<String, AnyObject>) ?? [:]

let overviewPolyline = (routes["overview_polyline"] as? Dictionary<String,AnyObject>) ?? [:]
let polypoints = (overviewPolyline["points"] as? String) ?? ""
let line = polypoints

self.addPolyLine(encodedString: line)
}
}
}

最佳答案

看来您是在URL中发送参数并使用JSON编码。 Alamofire请求的编码参数,将JSONEncoding.default修改为URLEncoding.default

同样来自 Google Map API Docs,它说如下。所以我相信,URL 编码将解决您的问题。

每个航路点可以是地名、地址或以逗号分隔的纬度/经度坐标。字符串应进行 URL 转义,因此应将“Berlin,Germany|Paris,France”等路标转换为 Berlin%2CGermany%7CParis%2CFrance。

关于ios - postman 响应正常,但在 Swift 3 上的 Alamofire 返回失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46180630/

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