如何解决无法从托管在 GitHub 上的 Json 读取数据
尝试从我的 GitHub 空间上的 json 读取数据时,出现错误
nw_protocol_get_quic_image_block_invoke dlopen libquic 失败 来自服务器的无效响应。请重试。
我是否使用了错误的 url 或者我解析数据的方式有问题?
我的回购网址
https://github.com/stark226/stark226.github.io.git
https://github.com/stark226/stark226.github.io/blob/3b2bebb4a3d85524732c7e7ec302b24f8d3e66ae/testjson.json
在我的视图中Didload
getDataFromJsonOnGithub(completed: { [weak self] result in
guard let self = self else {return}
switch result {
case .success(let expected):
print(expected)
case .failure(let error):
print(error.rawValue)
}
})
我的结构
struct RemoteData: Codable,Hashable {
var tokenDuration: Int
}
我的功能
func getDataFromJsonOnGithub(completed: @escaping (Result<RemoteData,listofErrors>) -> Void) {
let endpoint = "https://github.com/stark226/stark226.github.io/stark226/testjson.json"
guard let url = URL(string: endpoint) else {
completed(.failure(.invalidUsername))
return
}
let task = URLSession.shared.dataTask(with: url) { data,response,error in
if let _ = error {
completed(.failure(.unabletoComplete))
return
}
guard let response = response as? HTTPURLResponse,response.statusCode == 200 else {
completed(.failure(.invalidResponse))
return
}
guard let data = data else {
completed(.failure(.invalidData))
return
}
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let expectedRrsult = try decoder.decode(RemoteData.self,from: data)
completed(.success(expectedRrsult))
} catch {
completed(.failure(.invalidData))
}
}
task.resume()
}
enum listofErrors: String,Error {
case invalidUsername = "This username created an invalid request. Please try again."
case unabletoComplete = "Unable to complete your request. Please check your internet connection"
case invalidResponse = "Invalid response from the server. Please try again."
case invalidData = "The data received from the server was invalid. Please try again."
case unabletoFavorite = "There was an error favoriting this user. Please try again."
case alreadyInFavorites = "You've already favorited this user. You must REALLY like them!"
}
解决方法
您必须访问原始文件。
您访问的 URL 会呈现 HTML 页面。尝试以下网址(单击 GitHub 中的“Raw”按钮):
https://raw.githubusercontent.com/stark226/stark226.github.io/3b2bebb4a3d85524732c7e7ec302b24f8d3e66ae/testjson.json
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。