微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法从托管在 GitHub 上的 Json 读取数据

如何解决无法从托管在 GitHub 上的 Json 读取数据

尝试从我的 GitHub 空间上的 json 读取数据时,出现错误

nw_protocol_get_quic_image_block_invoke dlopen libquic 失败 来自服务器的无效响应。请重试。

我是否使用了错误的 url 或者我解析数据的方式有问题?

我的回购网址

https://github.com/stark226/stark226.github.io.git

在这个 url 上有一个简单的 json 文件

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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?