JSON URLComponents URLQueryItem 失败
JSON URLComponents URLQueryItem Failure
我正在努力处理 json 数据的 http 调用。如果我将整个 URL 用作字符串,
json 获取有效。如果我将调用分成 URL 个组件,我将无法
让它发挥作用。
我的网络服务:
final class Webservice {
var components: URLComponents {
var components = URLComponents()
components.scheme = "https"
components.host = "weather.visualcrossing.com"
components.queryItems =
[
URLQueryItem(name: "aggregateHours", value: "24"),
URLQueryItem(name: "combinationMethod", value: "aggregate"),
URLQueryItem(name: "startDateTime", value: "2020-08-03T00:00:00"),
URLQueryItem(name: "endDateTime", value: "2020-08-10T00:00:00"),
URLQueryItem(name: "collectStationContributions", value: "false"),
URLQueryItem(name: "maxStations", value: "-1"),
URLQueryItem(name: "maxDistance", value: "-1"),
URLQueryItem(name: "includeNormals", value: "false"),
URLQueryItem(name: "contentType", value: "json"),
URLQueryItem(name: "unitGroup", value: "us"),
URLQueryItem(name: "locationMode", value: "single"),
URLQueryItem(name: "key", value: "myPersonalKeyHere"),
URLQueryItem(name: "locations", value: "Findlay, OH"),
]
return components
}
let myURL = URL(string: "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/weatherdata/history?aggregateHours=24&combinationMethod=aggregate&startDateTime=2020-08-03T00%3A00%3A00&endDateTime=2020-08-10T00%3A00%3A00&collectStationContributions=false&maxStations=-1&maxDistance=-1&includeNormals=false&contentType=json&unitGroup=us&locationMode=single&key=myPersonalKeyHere&locations=Findlay%2C%20oh")
func fetchItems() -> AnyPublisher<ItemDataContainer, Error> {
//fix this - do not force unwrap
//Using myURL - it works
//return URLSession.shared.dataTaskPublisher(for: myURL!)
//using the components url it fails
return URLSession.shared.dataTaskPublisher(for: components.url!)
.map { [=11=].data }
.decode(type: ItemDataContainer.self, decoder: JSONDecoder())
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}//fetch
}//class
这是错误:
收到错误:,dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: “给定的数据无效 JSON。”, underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code= 3840 “字符 0 周围的值无效。”UserInfo={NSDebugDescription=字符 0 周围的值无效。})))
如有任何指导,我们将不胜感激。 Xcode11.6,iOS13.6
您缺少组件中的路径
components.path = "/VisualCrossingWebServices/rest/services/weatherdata/history"
我正在努力处理 json 数据的 http 调用。如果我将整个 URL 用作字符串, json 获取有效。如果我将调用分成 URL 个组件,我将无法 让它发挥作用。
我的网络服务:
final class Webservice {
var components: URLComponents {
var components = URLComponents()
components.scheme = "https"
components.host = "weather.visualcrossing.com"
components.queryItems =
[
URLQueryItem(name: "aggregateHours", value: "24"),
URLQueryItem(name: "combinationMethod", value: "aggregate"),
URLQueryItem(name: "startDateTime", value: "2020-08-03T00:00:00"),
URLQueryItem(name: "endDateTime", value: "2020-08-10T00:00:00"),
URLQueryItem(name: "collectStationContributions", value: "false"),
URLQueryItem(name: "maxStations", value: "-1"),
URLQueryItem(name: "maxDistance", value: "-1"),
URLQueryItem(name: "includeNormals", value: "false"),
URLQueryItem(name: "contentType", value: "json"),
URLQueryItem(name: "unitGroup", value: "us"),
URLQueryItem(name: "locationMode", value: "single"),
URLQueryItem(name: "key", value: "myPersonalKeyHere"),
URLQueryItem(name: "locations", value: "Findlay, OH"),
]
return components
}
let myURL = URL(string: "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/weatherdata/history?aggregateHours=24&combinationMethod=aggregate&startDateTime=2020-08-03T00%3A00%3A00&endDateTime=2020-08-10T00%3A00%3A00&collectStationContributions=false&maxStations=-1&maxDistance=-1&includeNormals=false&contentType=json&unitGroup=us&locationMode=single&key=myPersonalKeyHere&locations=Findlay%2C%20oh")
func fetchItems() -> AnyPublisher<ItemDataContainer, Error> {
//fix this - do not force unwrap
//Using myURL - it works
//return URLSession.shared.dataTaskPublisher(for: myURL!)
//using the components url it fails
return URLSession.shared.dataTaskPublisher(for: components.url!)
.map { [=11=].data }
.decode(type: ItemDataContainer.self, decoder: JSONDecoder())
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}//fetch
}//class
这是错误:
收到错误:,dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: “给定的数据无效 JSON。”, underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code= 3840 “字符 0 周围的值无效。”UserInfo={NSDebugDescription=字符 0 周围的值无效。})))
如有任何指导,我们将不胜感激。 Xcode11.6,iOS13.6
您缺少组件中的路径
components.path = "/VisualCrossingWebServices/rest/services/weatherdata/history"