我已经使用Alamofire
定义了要传递给RapidAPI的参数,但是出现错误。
我遵循了他们在API文档中所说的所有内容。如果我将完整的URL放入字符串中,则可以正常工作,但是当我将其作为参数传递时,
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
let CURRENT_BR_LEAGUE_URL = "https://api-football-v1.p.rapidapi.com/v2/leagues/country/"
override func viewDidLoad() {
super.viewDidLoad()
let params = ["country_name" : "england", "season" : "2018"]
getLeague(url: CURRENT_BR_LEAGUE_URL, parameters: params)
}
func getHeaders() -> [String : String] {
let headers = [
"X-RapidAPI-Host": "api-football-v1.p.rapidapi.com",
"X-RapidAPI-Key": "xxxxxxxxxxxxxxxxxxx"
]
return headers
}
func getLeague (url : String, parameters : [String : String]) {
Alamofire.request(url, method: .get, parameters: parameters, encoding: URLEncoding(destination: .queryString), headers: getHeaders()).responseJSON {
response in
if response.result.isSuccess {
let leagueJSON : JSON = JSON(response.result.value!)
print(leagueJSON)
print()
}
else {
}
}
}
}
它引发“错误的国家”错误。如果我使用完整网址
let CURRENT_BR_LEAGUE_URL = "https://api-football-v1.p.rapidapi.com/v2/leagues/country/england/2018"
正常。
如果我使用"https://api-football-v1.p.rapidapi.com/v2/leagues/country/"
并设置参数
let params = ["country_name" : "england", "season" : "2018"]
它不起作用
答案 0 :(得分:2)
在您的代码中,您正在获取请求。因此,您必须将参数绑定与基本URL一起传递。请尝试以下代码,希望它对您有用。
class ViewController: UIViewController {
let CURRENT_BR_LEAGUE_URL = "https://api-football-v1.p.rapidapi.com/v2/leagues/country/%@/%@"
override func viewDidLoad() {
super.viewDidLoad()
let params = ["country_name" : "england", "season" : "2018"]
let urlString = String(format: CURRENT_BR_LEAGUE_URL, params["country_name"],params["season"])
getLeague(url: urlString, parameters: nil)
}
func getHeaders() -> [String : String] {
let headers = [
"X-RapidAPI-Host": "api-football-v1.p.rapidapi.com",
"X-RapidAPI-Key": "xxxxxxxxxxxxxxxxxxx"
]
return headers
}
func getLeague (url : String, parameters : [String : String]?) {
Alamofire.request(url, method: .get, parameters: parameters, encoding: URLEncoding(destination: .queryString), headers: getHeaders()).responseJSON {
response in
if response.result.isSuccess {
let leagueJSON : JSON = JSON(response.result.value!)
print(leagueJSON)
print()
}
else {
}
}
}
}
答案 1 :(得分:0)
当我现在查看API-Football的RapidApi文档时,不需要参数。您只需要从不同的部分构造URL,然后发出GET请求即可。
class ViewController: UIViewController {
let MAIN_URL = "https://api-football-v1.p.rapidapi.com/v2/leagues/country/"
override func viewDidLoad() {
super.viewDidLoad()
getLeague(for: "england", year: 2018)
}
func getHeaders() -> [String : String] {
let headers = [
"X-RapidAPI-Host": "api-football-v1.p.rapidapi.com",
"X-RapidAPI-Key": "xxxxxxxxxxxxxxxxxxx"
]
return headers
}
func getLeague (for country : String, year: Int) {
let url = MAIN_URL + country + "/\(year)/"
Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: getHeaders()).responseJSON {
response in
if response.result.isSuccess {
let leagueJSON : JSON = JSON(response.result.value!)
print(leagueJSON)
print()
}
else {
}
}
}
}