let APIUrl = NSURL(string:"https://api.openweathermap.org/data/2.5/weather?lat=(currentLocation.coordinate.latitude)&lon=(currentLocation.coordinate.longitude)&appid=e7b2054dc37b1f464d912c00dd309595&units=Metric%22")
let request = URLRequest(url:APIUrl! as URL)
let dataTask = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error ?? "Error is empty.")
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse ?? "HTTP response is empty.")
}
guard let responseData = data else {
print("Error: did not receive data")
return
}
do {
let weatherData = try JSONDecoder().decode(MyWeather.self, from: responseData)
let ggtemp = weatherData.main?.temp
print(ggtemp!, "THIS IS THE TEMP")
DispatchQueue.main.async {
self.tempDisplay.text = String(format: "%.1f", ggtemp!)
}
} catch {
print("error parsing response from POST on /todos")
return
}
})
dataTask.resume()
}
我想获取当前位置的温度。 这是应用程序运行时得到的信息:
<NSHTTPURLResponse: 0x28322fbe0> { URL: https://api.openweathermap.org/data/2.5/weather?lat=(currentLocation.coordinate.latitude)&lon=(currentLocation.coordinate.longitude)&appid=e7b2054dc37b1f464d912c00dd309595&units=Metric%22 } { Status Code: 400, Headers {
"Access-Control-Allow-Credentials" = (
true
);
"Access-Control-Allow-Methods" = (
"GET, POST"
);
"Access-Control-Allow-Origin" = (
"*"
);
Connection = (
"keep-alive"
);
"Content-Length" = (
78
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Sun, 21 Oct 2018 09:57:38 GMT"
);
Server = (
openresty
);
"X-Cache-Key" = (
"/data/2.5/weather?lat=&lon=&units=Metric%22"
);
} }
error parsing response from POST on /todos
关于如何从此API获取临时提示的任何想法?
这是我的结构代码:
struct Coordinate : Decodable {
let lat, lon : Double?
}
struct Weather : Decodable {
var id : Int?
var main, myDescription, icon : String?
enum CodingKeys : String, CodingKey {
case id = "id"
case main = "main"
case icon = "icon"
case myDescription = "description"
}
}
struct Sys : Decodable {
let type, id : Int?
let sunrise, sunset : Date?
let message : Double?
let country : String?
}
struct Main : Decodable {
let temp : Double?
}
struct MyWeather : Decodable {
let coord : Coordinate?
let cod, visibility, id : Int?
let name : String?
let base : String?
let weather : [Weather]?
let sys : Sys?
let main : Main?
let dt : Date?
}
答案 0 :(得分:1)
您的代码正在解析数据。但是,我注意到您的URL units = Metric%22 的末尾应为 units = Metric 。另外,您的经纬度将不正确。 lat =(currentLocation.coordinate.latitude)应该为 lat = \(currentLocation.coordinate.latitude),与lon相同。
答案 1 :(得分:1)
我在邮递员中检查了您的api,得到了此回复
.feature {
display: flex;
align-items:center;
margin: 5px 0px;
}
.feature img {
display: inline-block;
width: 49%;
vertical-align:middle;
}
.feature .feature-detail {
display: inline-block;
width: 49%;
margin: auto 0px;
}
这是您使用此代码的完整API响应模型类
<div class="feature">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b3/Soylent.svg/800px-Soylent.svg.png">
<div class="feature-detail">
<h3>Feature Title</h3>
<p>Feature Description</p>
</div>
</div>