gpt4 book ai didi

ios - 没有从 API 获取所有数据

转载 作者:行者123 更新时间:2023-11-28 20:55:21 25 4
gpt4 key购买 nike

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?

}

最佳答案

我在 postman 中检查你的 api 得到这个响应

{
"coord": {
"lon": 90.36,
"lat": 23.79
},
"weather": [
{
"id": 721,
"main": "Haze",
"description": "haze",
"icon": "50d"
}
],
"base": "stations",
"main": {
"temp": 304.15,
"pressure": 1013,
"humidity": 62,
"temp_min": 304.15,
"temp_max": 304.15
},
"visibility": 3000,
"wind": {
"speed": 4.1,
"deg": 330
},
"clouds": {
"all": 20
},
"dt": 1540182600,
"sys": {
"type": 1,
"id": 7879,
"message": 0.0056,
"country": "BD",
"sunrise": 1540166342,
"sunset": 1540207601
},
"id": 1337178,
"name": "Dhaka District",
"cod": 200
}

这是您使用此代码的完整 API 响应模型类

   struct MyWeather : Decodable {
let coord : Coordinate?
let weather : [Weather]?
let base : String?
let main : Main?
let visibility: Int?
let wind : Wind?
let clouds : Clouds?
let dt : Date?
let sys : Sys?
let id : Int?
let name : String?
let cod : Int?
}

struct Coordinate : Decodable {
let lat : Double?
let 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 Main : Decodable {
let temp : Double?
let pressure : Int?
let humidity : Int?
let temp_min : Double?
let temp_max : Double?
}

struct Wind : Decodable {
let speed : Double?
let deg : Int?
}

struct Clouds: Decodable {
let all : Int?
}

struct Sys : Decodable {
let type : Int?
let id : Int?
let message : Double?
let country : String?
let sunrise : Date?
let sunset : Date?
}

关于ios - 没有从 API 获取所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52921569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com