[英]Parameters input from responseJSON to responseDecodable using Alamofire
I'm doing a.get request on Get Activity Streams (getActivityStreams) - Strava API using Alamofire.我正在使用 Alamofire 对Get Activity Streams (getActivityStreams) - Strava API执行 a.get 请求。
I was using the.responseJSON method but since it will be deprecated in Alamofire 6 I am trying to switch to using the.responseDecodable however I am facing some issues.我使用的是 .responseJSON 方法,但由于它将在 Alamofire 6 中被弃用,我试图切换到使用 the.responseDecodable 但是我遇到了一些问题。 I have to inputs specific parameters in the request as an array of string
我必须在请求中输入特定参数作为字符串数组
["keys": "latlng,time,altitude"]
This was my previous code that is functional这是我以前的功能代码
var headers : HTTPHeaders {
get {
return [
"Accept": "application/json",
]
}
}
let url_activity = "https://www.strava.com/api/v3/activities/IDXXXX/streams"
let params: [String: Any] = [ "keys" : "latlng,time,altitude"]
let head = HTTPHeaders(["Authorization" : "Bearer ACCESS_TOKEN"])
AF.request(url_activity, method: .get, parameters: params, headers: head).responseJSON {
response in
switch response.result {
case .success:
print(response.value)
break
case .failure(let error):
print(error)
}
}
I have declared a new struct and made it conform to the Encodable/Decodable protocol to handle these parameters and also used the encoder:JSONParameterEncoder.default
in the request like in the example我已经声明了一个新结构并使其符合 Encodable/Decodable 协议来处理这些参数,并且还在请求中使用了
encoder:JSONParameterEncoder.default
,就像在示例中一样
struct DecodableType: Codable {
let keys: [String: String]
}
let decode = DecodableType(keys: [ "keys" : ["latlng,time,altitude"]])
I have tried several versions like ["keys": "latlng,time,altitude"]
or ["keys": ["latlng","time","altitude"]]
without any success, here is the new request:我尝试了几个版本,如
["keys": "latlng,time,altitude"]
或["keys": ["latlng","time","altitude"]]
没有任何成功,这是新的请求:
AF.request(url_act,
method: .get,
parameters: decode,
encoder: JSONParameterEncoder.default ,
headers: head).responseDecodable(of: DecodableType.self) {
response in
// Same as before
Here is the error code, I know it's related to 2019 Apple policy that making a GET request with body data will fail with an error but I can't find a solution这是错误代码,我知道它与 2019 Apple 政策有关,即使用正文数据发出 GET 请求将失败并出现错误,但我找不到解决方案
urlRequestValidationFailed(reason: Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest(33 bytes))
urlRequestValidationFailed(原因:Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest(33 字节))
Thanks for you help !谢谢你的帮助!
I think you are mixing things up here.我想你在这里把事情搞混了。 You are implementing
Decodable
and talking/naming it that way, but what you are doing is Encoding
.您正在实现
Decodable
并以这种方式对其进行交谈/命名,但您正在做的是Encoding
。
It seems the Api expects a simple String
as paramter, constructing it with a [String: String]
seems ok.似乎 Api 需要一个简单的
String
作为参数,用[String: String]
构造它似乎没问题。 But what you are trying to send is JSON
.但是您要发送的是
JSON
。 I think the request is failing while trying to encode: ["keys": "latlng,time,altitude"]
to your custom struct.我认为请求在尝试将
["keys": "latlng,time,altitude"]
编码到您的自定义结构时失败。 Hence the error:因此错误:
urlRequestValidationFailed....bodyDataInGETRequest
urlRequestValidationFailed....bodyDataInGETRequest
As has allready been pointed out in the comments you would need to use your old constructor for sending the request.正如评论中已经指出的那样,您需要使用旧的构造函数来发送请求。
AF.request(url_activity, method: .get, parameters: params, headers: head)
then use the method:然后使用方法:
.responseDecodable(of: "what you expect in the response".self) {
Here you need to provide a custom struct that conforms to Decodable
and represents the >"returning json"<这里需要提供一个符合
Decodable
的自定义结构体,代表>"returning json"<
For the request you linked it would be:对于您链接的请求,它将是:
struct ResponseElement: Codable {
let type: String
let data: [Double]
let seriesType: String
let originalSize: Int
let resolution: String
enum CodingKeys: String, CodingKey {
case type, data
case seriesType = "series_type"
case originalSize = "original_size"
case resolution
}
}
and decoding with:和解码:
.responseDecodable(of: [ResponseElement].self) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.