如何让 Http get request URL 由 swift 中的管道组成 3?
How to make Http get request URL consist of pipelines in swift 3?
我已从 swift 发出获取请求 3. url 有管道。没有管道它工作正常但是当我添加管道时代码中断说在展开可选值错误时发现 nil。
这是我的 url
让 url = URL(字符串:"https://maps.googleapis.com/maps/api/directions/json?origin=colombo&destination=kandy&waypoints=optimize:true|&key=AIzaSyCNBcQLIVvNwqjkYiLawnYK_AM")
这是我的代码:
func synchronusGetRequstForExternalAPI(api_url:String, headers:[ String: String]) -> ResultModel {
let resultModel = ResultModel()
//create the url with URL
let url = URL(string:"https://maps.googleapis.com/maps/api/directions/json?origin=colombo&destination=kandy&waypoints=optimize:true|&key=AIzaSyCNBcQLIVvNwqjkYiLawnYK_OK4EQbRM5M")
//create the URLRequest object using the url object
var request = URLRequest(url: url!)
//set headers
for item in headers {
request.addValue(item.value, forHTTPHeaderField: item.key)
}
let semaphore = DispatchSemaphore(value: 0)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
(data, response, error) in
if(error != nil){
resultModel.ErrorType = .NO_INT
resultModel.JsonReslut = JSON.null
}else{
if let resp = response as? HTTPURLResponse{
if(resp.statusCode == 200){
if let jsonResult = JSON(data) as? JSON {
resultModel.ErrorType = .NO_ERROR
resultModel.JsonReslut = jsonResult
}
}else{
if let jsonResult = JSON(data) as? JSON {
resultModel.ErrorType = .SEREVR_ERROR
resultModel.JsonReslut = jsonResult
}else{
resultModel.ErrorType = .SEREVR_ERROR
resultModel.JsonReslut = JSON.null
}
}
}
}
semaphore.signal()
}
task.resume()
_ = semaphore.wait(timeout: DispatchTime.distantFuture)
return resultModel
}
谁能告诉我为什么会这样?
|
不是 URL 中允许的字符。尝试对 URL 字符串
进行百分比转义
let url = URL(string:"https://maps.googleapis.com/maps/api/directions/json?origin=colombo&destination=kandy&waypoints=optimize:true|&key=AIzaSyCNBcQLIVvNwqjkYiLawnYK_OK4EQbRM5M".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet()))
我已从 swift 发出获取请求 3. url 有管道。没有管道它工作正常但是当我添加管道时代码中断说在展开可选值错误时发现 nil。
这是我的 url 让 url = URL(字符串:"https://maps.googleapis.com/maps/api/directions/json?origin=colombo&destination=kandy&waypoints=optimize:true|&key=AIzaSyCNBcQLIVvNwqjkYiLawnYK_AM")
这是我的代码:
func synchronusGetRequstForExternalAPI(api_url:String, headers:[ String: String]) -> ResultModel {
let resultModel = ResultModel()
//create the url with URL
let url = URL(string:"https://maps.googleapis.com/maps/api/directions/json?origin=colombo&destination=kandy&waypoints=optimize:true|&key=AIzaSyCNBcQLIVvNwqjkYiLawnYK_OK4EQbRM5M")
//create the URLRequest object using the url object
var request = URLRequest(url: url!)
//set headers
for item in headers {
request.addValue(item.value, forHTTPHeaderField: item.key)
}
let semaphore = DispatchSemaphore(value: 0)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
(data, response, error) in
if(error != nil){
resultModel.ErrorType = .NO_INT
resultModel.JsonReslut = JSON.null
}else{
if let resp = response as? HTTPURLResponse{
if(resp.statusCode == 200){
if let jsonResult = JSON(data) as? JSON {
resultModel.ErrorType = .NO_ERROR
resultModel.JsonReslut = jsonResult
}
}else{
if let jsonResult = JSON(data) as? JSON {
resultModel.ErrorType = .SEREVR_ERROR
resultModel.JsonReslut = jsonResult
}else{
resultModel.ErrorType = .SEREVR_ERROR
resultModel.JsonReslut = JSON.null
}
}
}
}
semaphore.signal()
}
task.resume()
_ = semaphore.wait(timeout: DispatchTime.distantFuture)
return resultModel
}
谁能告诉我为什么会这样?
|
不是 URL 中允许的字符。尝试对 URL 字符串
let url = URL(string:"https://maps.googleapis.com/maps/api/directions/json?origin=colombo&destination=kandy&waypoints=optimize:true|&key=AIzaSyCNBcQLIVvNwqjkYiLawnYK_OK4EQbRM5M".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet()))