在传递正确的URL

时间:2017-11-08 11:02:09

标签: get alamofire ios11

我想要做的是从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上工作...... 我认为这是因为'标记 我可以看到\是嵌入式说FATAL无效网址

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

2 个答案:

答案 0 :(得分:1)

正如我所见,您的URL字符串包含select query中的空格。

实际上,alamofire不支持带空格的URL字符串。尝试用%20替换URL字符串空格(即编码字符空间)。

或者您可以将URL编码与Alamofire请求一起使用。

希望它会有所帮助!

答案 1 :(得分:0)

我用这个:

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)
}