Alamofire 将应用程序 API 放在 url 的开头而不是结尾
Alamofire places the application API at the beginning of url rather than end of it
我正在构建一个简单的天气应用程序,没有什么复杂的,我只需要根据用户位置从 Open Weather Map 中获取 JSON。这是从 WOM http://api.openweathermap.org/data/2.5/weather?lat=52.516221&lon=13.408363&appid=e72ca729af228beabd5d20e3b7749713
中获取 JSON 的正确 URL 结构
然而,这就是我的 swift 代码/Alamofire 给我的 http://api.openweathermap.org/data/2.5/weather?appid=e72ca729af228beabd5d20e3b7749713&lat=52.516221&long=13.408363
所以它将 apiid=***
放在 url 的开头而不是结尾。
这是我的代码。
let WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather"
let APP_ID = "e72ca729af228beabd5d20e3b7749713"
func getWeatherData(url: String, parameters : [String : String]) {
Alamofire.request(url, method: .get, parameters: parameters).responseJSON {
response in
if response.result.isSuccess {
print ("Everything is fine")
print ( Alamofire.request(url, method: .get, parameters: parameters))
let weatherJSON : JSON = JSON(response.result.value!)
print (weatherJSON)
}
else {
print ("Error \(String(describing: response.result.error))")
self.cityLabel.text = "Connection issues"
}
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
let latitude = "52.516221"
let longitude = "13.408363"
let params : [String : String] = ["lat" : latitude, "long" : longitude, "appid" : APP_ID]
getWeatherData(url : WEATHER_URL, parameters : params)
}
}
REST API 中的参数顺序无关紧要。
您的代码存在问题,因为您传递了错误的参数名称。 long
应该是 lon
.
以上两个请求都会给您相同的结果。
试试这个。我刚刚将参数 'long' 更改为 lon
.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
let latitude = "52.516221"
let longitude = "13.408363"
let params : [String : String] = ["lat" : latitude, "lon" : longitude, "appid" : APP_ID]
getWeatherData(url : WEATHER_URL, parameters : params)
}
}
我正在构建一个简单的天气应用程序,没有什么复杂的,我只需要根据用户位置从 Open Weather Map 中获取 JSON。这是从 WOM http://api.openweathermap.org/data/2.5/weather?lat=52.516221&lon=13.408363&appid=e72ca729af228beabd5d20e3b7749713
然而,这就是我的 swift 代码/Alamofire 给我的 http://api.openweathermap.org/data/2.5/weather?appid=e72ca729af228beabd5d20e3b7749713&lat=52.516221&long=13.408363
所以它将 apiid=***
放在 url 的开头而不是结尾。
这是我的代码。
let WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather"
let APP_ID = "e72ca729af228beabd5d20e3b7749713"
func getWeatherData(url: String, parameters : [String : String]) {
Alamofire.request(url, method: .get, parameters: parameters).responseJSON {
response in
if response.result.isSuccess {
print ("Everything is fine")
print ( Alamofire.request(url, method: .get, parameters: parameters))
let weatherJSON : JSON = JSON(response.result.value!)
print (weatherJSON)
}
else {
print ("Error \(String(describing: response.result.error))")
self.cityLabel.text = "Connection issues"
}
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
let latitude = "52.516221"
let longitude = "13.408363"
let params : [String : String] = ["lat" : latitude, "long" : longitude, "appid" : APP_ID]
getWeatherData(url : WEATHER_URL, parameters : params)
}
}
REST API 中的参数顺序无关紧要。
您的代码存在问题,因为您传递了错误的参数名称。 long
应该是 lon
.
以上两个请求都会给您相同的结果。
试试这个。我刚刚将参数 'long' 更改为 lon
.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
let latitude = "52.516221"
let longitude = "13.408363"
let params : [String : String] = ["lat" : latitude, "lon" : longitude, "appid" : APP_ID]
getWeatherData(url : WEATHER_URL, parameters : params)
}
}