Alamofire无效的网址

问题描述 投票:0回答:1

在尝试获取信息时获取错误。服务器支持URL中的俄语。

错误:

[结果]:FAILURE:invalidURL(url:“http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=сплин&api_key=bad5acca27008a09709ccb2c0258003b&format=json”)

请求:

Alamofire.request("http://ws.audioscrobbler.com/2.0/?    method=artist.search&artist=\(nameOfArtist)&api_key=bad5acca27008a09709ccb2c0258003b&format=json")
            .responseObject { (response: DataResponse<SearchArtistAPIModel>) in
                //to get status code
                debugPrint(response)
                if let status = response.response?.statusCode {

                    switch(status){

                    case 200...499:
                        print("example success")
                    default:
                        print("error with response status: \(status)")
                    }
                }

                if let artistName = response.result.value {

                    guard let artistsArray = artistName.results?.artistmatches?.artist else { return }
                    var names: [String] = []
                    for artists in artistsArray {
                        guard let artistsName = artists.name else { return }
                        names.append(artistsName)
                        completion(names)
                    }
                }
        }
swift alamofire
1个回答
0
投票

编码你的URL然后创建请求。

let urlString = "http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=\(nameOfArtist)&api_key=bad5acca27008a09709ccb2c0258003b&format=json"        
if let encoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),let url = URL(string: encoded)
 {
     Alamofire.request(url).validate().responseJSON { (json) in
         print(json)
         //Enter your code here
     }
}
© www.soinside.com 2019 - 2024. All rights reserved.