API 请求没有给出响应
Posted
技术标签:
【中文标题】API 请求没有给出响应【英文标题】:API request isn't giving a response 【发布时间】:2020-02-24 02:39:17 【问题描述】:我正在尝试使用 (https://collegescorecard.ed.gov/data/documentation/) 在我的 ios 应用程序中填充表格,但没有任何数据显示。我已经尝试了很多次我的 api 密钥和 url,所以我不认为它是那样的。 json文件的结构
开发类学校
名称字符串 IPEDS 中报告的机构名称 (INSTNM)。
import UIKit
import Alamofire
class ViewController: UIViewController
let URLStr = "https://api.data.gov/ed/collegescorecard/v1/schools?fields=school.name"
var resultArray = [Users]()
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var searchCollege = [String]()
override func viewDidLoad()
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
callAPI()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
func callAPI()
let key = "key redacted"
//let url = URL(string:"\(URLStr)?api_key=\(key)")
let url = URL(string:"\(URLStr)&api_key=\(key)")
print(url)
Alamofire.request(url!, method: .get, parameters: nil, headers:[:]).responseJSON (response) in
if let responseObj = response.value as? [[String: Any]]
self.resultArray = self.parseJSON(userData: responseObj)
print("response here", response)
self.tableView.reloadData()
func parseJSON(userData: [[String:Any]])-> [Users]
var userArr = [Users]()
for obj in userData
do let data = try JSONSerialization.data(withJSONObject: obj, options: .prettyPrinted)
let decodedData = try JSONDecoder().decode(Users.self, from: data)
userArr.append(decodedData)
catch
return userArr
userModal.swift
mport Foundation
struct Users: Codable
let userSchool_name: String?
enum CodingKeys: String, CodingKey
case userSchool_name = "school.name"
我试图打印出响应和不同的对象,但没有打印出来。我在下面有一个函数可以在应用程序中显示它,但我认为这不是问题。
json 输出
SUCCESS:
metadata =
page = 0;
"per_page" = 20;
total = 7112;
;
results = (
"school.name" = "West Michigan College of Barbering and Beauty";
,
"school.name" = "Georgia College & State University";
,
"school.name" = "Georgia Southern University";
,
"school.name" = "Clayton State University";
,
"school.name" = "Andrew College";
,
"school.name" = "Columbus Technical College";
,
"school.name" = "Georgia Northwestern Technical College";
,
"school.name" = "Atlanta Metropolitan State College";
,
"school.name" = "Clark Atlanta University";
,
"school.name" = "Augustana College";
,
"school.name" = "Miami Ad School at Portfolio Center";
,
"school.name" = "Tricoci University of Beauty Culture-Urbana";
,
"school.name" = "University of Hawaii at Manoa";
,
"school.name" = "Aveda Institute-Twin Falls";
,
"school.name" = "Cameo Beauty Academy";
,
"school.name" = "Truett McConnell University";
,
"school.name" = "Cannella School of Hair Design-Chicago";
,
"school.name" = "Valdosta State University";
,
"school.name" = "American Academy of Art";
,
"school.name" = "City Colleges of Chicago-Kennedy-King College";
);
【问题讨论】:
是否打印url
?
您是否尝试在条件if let responseObj = response.value as? [[String: Any]]
之前打印响应
@Rob 的 url 确实打印出来了,但是谢谢
@jenna 好的,parseJSON
成功了吗?
@Joshua 它确实在 if let responseObj = response.value as? [[String: Any]]
之前打印,所以我认为它没有成功地执行 parseJSON @Rob
【参考方案1】:
首先删除parseJSON
,把JSON转成数组,再转回JSON,再转成带有JSONDecoder
的struct是没有意义的
罢工>
func parseJSON(userData: [[String:Any]])-> [Users]
var userArr = [Users]()
for obj in userData
do let data = try JSONSerialization.data(withJSONObject: obj, options: .prettyPrinted)
let decodedData = try JSONDecoder().decode(Users.self, from: data)
userArr.append(decodedData)
catch
return userArr
然后你必须为根对象添加一个结构
struct Root: Codable
let results : [Users]
struct Users: Codable
let userSchool_name: String?
private enum CodingKeys: String, CodingKey
case userSchool_name = "school.name"
并将 Alamofire 部分替换为
Alamofire.request(url!).responseData response in
switch response.result
case .success(let data):
do
self.resultArray = try JSONDecoder().decode(Root.self, from: data).results
self.tableView.reloadData()
catch
print(error)
case .failure(let error):
print(error)
它接收Data
而不是解码的数组并处理所有错误。
【讨论】:
感谢您的输入,但我收到此错误,typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array 但是而是找到了字典。”,基础错误:无)) 谢谢-> 但是它给出的值是 nil,print(self.resultArray) 给出 Application.Users(userSchool_name: nil),但是对于正确数量的学校CodingKeys
枚举必须在 Users
结构内,您可以声明 userSchool_name
非可选和 lowerCamelCased。以上是关于API 请求没有给出响应的主要内容,如果未能解决你的问题,请参考以下文章
axios在20秒内给出响应,但具有相同请求有效载荷的相同api在6秒内从邮递员那里得到响应