【问题标题】:JSON URLComponents URLQueryItem FailureJSON URLComponents URLQueryItem 失败
【发布时间】:2020-12-06 03:38:26
【问题描述】:

我正在为 json 数据的 http 调用而苦苦挣扎。如果我使用整个 URL 作为字符串, json fetch 有效。如果我将调用分成 URLComponents 我无法 让它发挥作用。

我的网络服务:

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 { $0.data }
            .decode(type: ItemDataContainer.self, decoder: JSONDecoder())
            .receive(on: DispatchQueue.main)
            .eraseToAnyPublisher()
    }//fetch

}//class

这是错误:

收到一个错误:,dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=字符 0.})))

任何指导将不胜感激。 Xcode 11.6、iOS 13.6

【问题讨论】:

  • 您应该尝试使用 MITMProxy 之类的工具拦截您的请求和响应,以确定您是否按预期发送数据以及返回的内容。
  • 如果您打印出URLComponents 生成的url 和手动构建的url,您会发现某些参数存在差异。这可能会导致来自 API 的错误回复无法解码为 ItemDataContainer

标签: ios json xcode urlcomponents


【解决方案1】:

您在组件中缺少路径

components.path = "/VisualCrossingWebServices/rest/services/weatherdata/history"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 2013-09-29
    • 2019-07-21
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多