Alamofire 黑暗天空 API 请求 Swift
Alamofire Dark Sky API request Swift
我正在尝试完成对黑暗天空的 .get 请求 API 需要以下参数才能完成请求:https://api.darksky.net/forecast/[key]/[latitude],[longitude]
我的代码如下所示,我在 'method' 调用中得到一个额外的参数,我认为这是因为我的函数数据类型错误。我一直在尝试四处寻找正确的,但我无法找到正确的答案。
我的Alamofire请求如下:
//Get wind data method
func getWindData(url: String, key: Any, latitude: Any, longitude: Any) {
Alamofire.request(url, method: .get, key, latitude, longitude).responseJSON {
response in
if response.result.isSuccess {
print("Success! Got the weather data")
let windJSON : JSON = JSON(response.result.value!)
print(windJSON)
}
else {
print("Error \(String(describing: response.result.error))") }
self.yard.text = "Connection issues"
}
}
而我的'Did update method'如下使用phone gps获取用户所需的经纬度:
//Did update method
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
self.locationManager.stopUpdatingLocation()
print("latitude = \(location.coordinate.latitude), longitude = \(location.coordinate.longitude)")
let latitude = String(location.coordinate.latitude)
let longitude = String(location.coordinate.longitude)
getWindData(url: base_URL, key: api_Key, latitude: latitude, longitude: longitude)
}
}
//Did fail with error method
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
yard.text = "Error"
}
非常感谢!亚历克斯
你需要
let openWeatherMapBaseURL = "https://api.darksky.net/forecast/Key_Here/"
let urlStr = "\(openWeatherMapBaseURL)\(latitude),\(longitude)"
Alamofire.request(urlStr, method: .get, parameters:nil, encoding: JSONEncoding.default).responseJSON { [weak self] response in
}
不存在
Alamofire.request(url, method: .get, key, latitude, longitude).responseJSON
get 的参数需要在 url 中,而且 latitude
和 longitude
是 double
值而不是 Any
我正在尝试完成对黑暗天空的 .get 请求 API 需要以下参数才能完成请求:https://api.darksky.net/forecast/[key]/[latitude],[longitude]
我的代码如下所示,我在 'method' 调用中得到一个额外的参数,我认为这是因为我的函数数据类型错误。我一直在尝试四处寻找正确的,但我无法找到正确的答案。
我的Alamofire请求如下:
//Get wind data method
func getWindData(url: String, key: Any, latitude: Any, longitude: Any) {
Alamofire.request(url, method: .get, key, latitude, longitude).responseJSON {
response in
if response.result.isSuccess {
print("Success! Got the weather data")
let windJSON : JSON = JSON(response.result.value!)
print(windJSON)
}
else {
print("Error \(String(describing: response.result.error))") }
self.yard.text = "Connection issues"
}
}
而我的'Did update method'如下使用phone gps获取用户所需的经纬度:
//Did update method
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
self.locationManager.stopUpdatingLocation()
print("latitude = \(location.coordinate.latitude), longitude = \(location.coordinate.longitude)")
let latitude = String(location.coordinate.latitude)
let longitude = String(location.coordinate.longitude)
getWindData(url: base_URL, key: api_Key, latitude: latitude, longitude: longitude)
}
}
//Did fail with error method
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
yard.text = "Error"
}
非常感谢!亚历克斯
你需要
let openWeatherMapBaseURL = "https://api.darksky.net/forecast/Key_Here/"
let urlStr = "\(openWeatherMapBaseURL)\(latitude),\(longitude)"
Alamofire.request(urlStr, method: .get, parameters:nil, encoding: JSONEncoding.default).responseJSON { [weak self] response in
}
不存在
Alamofire.request(url, method: .get, key, latitude, longitude).responseJSON
get 的参数需要在 url 中,而且 latitude
和 longitude
是 double
值而不是 Any