gpt4 book ai didi

ios - 为什么使用 Alamofire 请求失败?

转载 作者:太空宇宙 更新时间:2023-11-03 13:57:03 25 4
gpt4 key购买 nike

我想获取github/users的数据,我尝试了两种方法:

第一种方法成功:

AF.request("https://api.github.com/users").responseJSON { response in
print("UOUOUOUOUOU", response.description)
}

但是第二次失败了,为什么?

Network.request(Request.users).responseDecodable { (response: AFDataResponse<UserList>) in
switch response.result {
case .success(let value):
print("SUCCESS")
case .failure(let error):
print("FAIL")
}
}

我需要使用第二种方法。

这是我的类(class)网络

class Network {
let session: Session
let evaluators = ["api.github.com/users": PinnedCertificatesTrustEvaluator(certificates: [
Certificates.github
])
]

private init() {
session = Session(serverTrustManager: ServerTrustManager(evaluators: evaluators)
)
}

private static let shared = Network()
static func request(_ convertible: URLRequestConvertible) -> DataRequest {
return shared.session.request(convertible)
}
}

我收到这个错误:

FAIL serverTrustEvaluationFailed(reason: Alamofire.AFError.ServerTrustFailureReason.noRequiredEvaluator(host: "api.github.com"))
2019-12-17 12:03:21.649222-0400 SSLOwner[5533:83111] Task <57DC5433-F132-4453-BB68-CCF90B5F6058>.<1> HTTP load failed, 0/0 bytes (error code: -999 [1:89])

编辑 1:这是我的 Struct UserList 和 User

import Foundation
struct UserList: Codable {
let users: [User]

enum CodingKeys: String, CodingKey {
case users = "items"
}
}


struct User: Codable {
let displayName: String

}

session = Session()

我收到这个错误:

responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary but found an array instead.", underlyingError: nil))))

最佳答案

您的上一个错误明确说明了错误所在。 API 调用的响应是一个数组,而不是您假设的字典。将您的预期响应转换为用户列表,然后它应该可以工作:

Network.request(Request.users).responseDecodable { (response: AFDataResponse<[User]>) in
switch response.result {
case .success(let value):
print("SUCCESS")
case .failure(let error):
print("FAIL")
}
}

关于ios - 为什么使用 Alamofire 请求失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59377856/

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