有没有办法将 API 中的内容打印到本地主机服务器?
Is there a way to print the content from the API to the localhost server?
我正在尝试学习spring,同时构建一个小项目,但遇到了问题。一切正常,但我不知道如何使用 Eclipse 控制台中打印的 API 方法内容。
在我的控制器中,我拥有使用 API 的特定方法所需的所有信息。我使用 "@GetMapping("/seasons") 映射了 URL,我使用的是来自在线 API(带密钥)的代码片段。并且在 VIEW 文件夹中(基本上是我保存 JSP 文件的地方,例如:seasons.jsp)我正在尝试从 API.
的响应中检索数据
这是 API 的回复:“{"get":"seasons","parameters":[],"errors":[],"results":10,"response":[2012,2013,2014,2015,2016,2017,2018,2019,2020,2021]}
”
更新:
这里有一些代码供参考:
@GetMapping("/seasons")
public String seasons(Model theModel) throws IOException, InterruptedException {
List<Integer> SeasonsList = new ArrayList<>();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api-formula-1.p.rapidapi.com/seasons"))
.header("x-rapidapi-host", "api-formula-1.p.rapidapi.com")
.header("x-rapidapi-key", "5a6f44fa10msh40be2be8d20bc5bp18a190jsnb4478dcbc8f1")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
JSONObject seasonsObject = new JSONObject(response.body());
for(int i = 0; i < seasonsObject.length(); i++) {
JSONArray object = seasonsObject.getJSONArray("response");
for(int j = 0 ; i < object.length(); j++) {
SeasonsList.add(object.getInt(j));
}
System.out.println(object);
}
theModel.addAttribute("theSeasons", SeasonsList);
return "seasons";
}
和 HTML 文件:
<html xmlns:th ="http://www.thymeleaf.org">
<head>
<title>The Formula 1 Seasons are</title>
</head>
<body>
<p th:text ="'The seasons are: ' + ${theSeasons}"/>
</body>
</html>
我想要的是显示“季节是:2012、2013、2014 等
我在控制台中收到错误消息:“org.json.JSONException: JSONArray[10] 未找到。”
如果您需要我的项目的任何详细信息,请告诉我。
您的内部循环使用了错误的循环变量并将 j
移动到数组末尾之后:
// Don't do this
for (int j = 0 ; i < object.length(); j++) { // Comparing "i"; compare "j" instead
// Do this
for (int j = 0 ; j < object.length(); j++) {
虽然还不清楚为什么需要 outer 循环;您正在从已知响应中提取已知 属性;没有理由使用外循环 AFAICT。
我正在尝试学习spring,同时构建一个小项目,但遇到了问题。一切正常,但我不知道如何使用 Eclipse 控制台中打印的 API 方法内容。
在我的控制器中,我拥有使用 API 的特定方法所需的所有信息。我使用 "@GetMapping("/seasons") 映射了 URL,我使用的是来自在线 API(带密钥)的代码片段。并且在 VIEW 文件夹中(基本上是我保存 JSP 文件的地方,例如:seasons.jsp)我正在尝试从 API.
的响应中检索数据这是 API 的回复:“{"get":"seasons","parameters":[],"errors":[],"results":10,"response":[2012,2013,2014,2015,2016,2017,2018,2019,2020,2021]}
”
更新:
这里有一些代码供参考:
@GetMapping("/seasons")
public String seasons(Model theModel) throws IOException, InterruptedException {
List<Integer> SeasonsList = new ArrayList<>();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api-formula-1.p.rapidapi.com/seasons"))
.header("x-rapidapi-host", "api-formula-1.p.rapidapi.com")
.header("x-rapidapi-key", "5a6f44fa10msh40be2be8d20bc5bp18a190jsnb4478dcbc8f1")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
JSONObject seasonsObject = new JSONObject(response.body());
for(int i = 0; i < seasonsObject.length(); i++) {
JSONArray object = seasonsObject.getJSONArray("response");
for(int j = 0 ; i < object.length(); j++) {
SeasonsList.add(object.getInt(j));
}
System.out.println(object);
}
theModel.addAttribute("theSeasons", SeasonsList);
return "seasons";
}
和 HTML 文件:
<html xmlns:th ="http://www.thymeleaf.org">
<head>
<title>The Formula 1 Seasons are</title>
</head>
<body>
<p th:text ="'The seasons are: ' + ${theSeasons}"/>
</body>
</html>
我想要的是显示“季节是:2012、2013、2014 等
我在控制台中收到错误消息:“org.json.JSONException: JSONArray[10] 未找到。”
如果您需要我的项目的任何详细信息,请告诉我。
您的内部循环使用了错误的循环变量并将 j
移动到数组末尾之后:
// Don't do this
for (int j = 0 ; i < object.length(); j++) { // Comparing "i"; compare "j" instead
// Do this
for (int j = 0 ; j < object.length(); j++) {
虽然还不清楚为什么需要 outer 循环;您正在从已知响应中提取已知 属性;没有理由使用外循环 AFAICT。