向右 url 传递时 IOS 中的 alamofire 无效 URL 错误
Invalid URL Error in alamofire in IOS while passing right url
我想做的是从下面url
获取json数据
https://query.yahooapis.com/v1/public/yql?q=select * from
weather.forecast where woeid in (select woeid from geo.places(1) where
text='seoul,kr') and u='c'&format=json
如果在浏览器中输入,效果会很好。
但它在 alamofire 上不起作用...
我想是因为 ' mark
如我所见,\ 嵌入说 FATAL invalid url
let rrrr="https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='seoul,kr') and u='c'&format=json"
print(rrrr)
let alamo=Alamofire.request(rrrr,method:.get)
alamo.responseJSON {
response in
if response.data != nil {
print("not null")
let json = JSON(data: response.data!)
print(json)
print("not null21")
print(response)
print("not null12")
print(response.data!)
日志结果如下
https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='seoul,kr') and u='c'&format=json
not null
null
not null21
FAILURE: invalidURL("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\'seoul,kr\') and u=\'c\'&format=json")
not null12
正如我所见,您的 URL 字符串包含 select 查询中的 space。
实际上,alamofire 不支持带有 space 的 URL 字符串。尝试将 URL 字符串 spaces 替换为 %20
(即编码字符 space)。
或者您可以对 Alamofire 请求使用 URL 编码。
希望对您有所帮助!
我用这个:
enum ConsultOfficesServiceEndpoints : String {
case getProposedPeople = "aaa/bbb/ccc/%d/"
}
.
.
.
func getProposedPeople(_ name: String, success: @escaping SuccessResponse,failure: @escaping FailureResponse){
var paramWithSpaces = ""
var lines = name.components(separatedBy: " ")
var numlines = lines.count
var i = 0
if numlines > 1{
while i < (numlines - 1) {
paramWithSpaces = paramWithSpaces + lines[i] + "%20"
i = i+1
}
paramWithSpaces = paramWithSpaces + lines[numlines - 1]
}else{
paramWithSpaces = name
}
self.GET(withEndpoint: ConsultOfficesServiceEndpoints.getProposedPeople.rawValue.replacingOccurrences(of: "%d", with: paramWithSpaces), params: nil, headers: nil, success: success, failure: failure)
}
我想做的是从下面url
获取json数据https://query.yahooapis.com/v1/public/yql?q=select * from
weather.forecast where woeid in (select woeid from geo.places(1) where
text='seoul,kr') and u='c'&format=json
如果在浏览器中输入,效果会很好。
但它在 alamofire 上不起作用... 我想是因为 ' mark 如我所见,\ 嵌入说 FATAL invalid url
let rrrr="https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='seoul,kr') and u='c'&format=json"
print(rrrr)
let alamo=Alamofire.request(rrrr,method:.get)
alamo.responseJSON {
response in
if response.data != nil {
print("not null")
let json = JSON(data: response.data!)
print(json)
print("not null21")
print(response)
print("not null12")
print(response.data!)
日志结果如下
https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='seoul,kr') and u='c'&format=json
not null
null
not null21
FAILURE: invalidURL("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\'seoul,kr\') and u=\'c\'&format=json")
not null12
正如我所见,您的 URL 字符串包含 select 查询中的 space。
实际上,alamofire 不支持带有 space 的 URL 字符串。尝试将 URL 字符串 spaces 替换为 %20
(即编码字符 space)。
或者您可以对 Alamofire 请求使用 URL 编码。
希望对您有所帮助!
我用这个:
enum ConsultOfficesServiceEndpoints : String {
case getProposedPeople = "aaa/bbb/ccc/%d/"
}
.
.
.
func getProposedPeople(_ name: String, success: @escaping SuccessResponse,failure: @escaping FailureResponse){
var paramWithSpaces = ""
var lines = name.components(separatedBy: " ")
var numlines = lines.count
var i = 0
if numlines > 1{
while i < (numlines - 1) {
paramWithSpaces = paramWithSpaces + lines[i] + "%20"
i = i+1
}
paramWithSpaces = paramWithSpaces + lines[numlines - 1]
}else{
paramWithSpaces = name
}
self.GET(withEndpoint: ConsultOfficesServiceEndpoints.getProposedPeople.rawValue.replacingOccurrences(of: "%d", with: paramWithSpaces), params: nil, headers: nil, success: success, failure: failure)
}