java - 从 RESTful API 检索内容到 java 桌面应用程序中的表
问题描述
所以我正在开发一个 java 桌面应用程序,我正在寻找有关如何从我正在使用的 RESTful API 将信息检索到 jtable 的信息。我已经搜索过了,但我没有找到任何与我的问题相关的信息然而。我可以为此提供一些指导以帮助我解决问题吗?当然,我不想得到解决方案,我只是想要一些关于在哪里搜索和搜索什么的反馈。谢谢!这是我到目前为止实施的
public class Stocks extends javax.swing.JFrame {
public Stocks() {
initComponents();
String url = "http://localhost:8080/service/webresources/test.stocks";
URL obj = null;
try {
obj = new URL(url);
} catch (MalformedURLException ex) {
Logger.getLogger(Stocks.class.getName()).log(Level.SEVERE, null, ex);
}
HttpURLConnection con = null;
try {
con = (HttpURLConnection) obj.openConnection();
} catch (IOException ex) {
Logger.getLogger(Stocks.class.getName()).log(Level.SEVERE, null, ex);
}
try {
// optional default is GET
con.setRequestMethod("GET");
} catch (ProtocolException ex) {
Logger.getLogger(Stocks.class.getName()).log(Level.SEVERE, null, ex);
}
con.setRequestProperty("Accept", "application/json");
StringBuilder response = null;
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))) {
String inputLine;
response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
} catch (IOException ex) {
Logger.getLogger(Stocks.class.getName()).log(Level.SEVERE, null, ex);
}
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(response.toString());
} catch (JSONException ex) {
Logger.getLogger(Stocks.class.getName()).log(Level.SEVERE, null, ex);
}
String stockName = "";
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsnObj = null;
try {
jsnObj = (JSONObject) jsonArray.get(i);
} catch (JSONException ex) {
Logger.getLogger(Stocks.class.getName()).log(Level.SEVERE, null, ex);
}
try {
stockName = (String) jsnObj.get("stockName");
} catch (JSONException ex) {
Logger.getLogger(Stocks.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(stockName);
}
}
这就是我现在所拥有的,我从 API 获得 JSON 响应,现在我打印 JSON 的元素之一。如何将此响应转换为表格?
解决方案
推荐阅读
- r - 在mice.2l.norm 中出现错误“无类变量”
- bash - 运行 Git 命令时无法读取“”
- c - 在 R stats 包中查找 cor() 函数的 C 源代码
- python - 套接字聊天室 - python 3.7
- python - LinearSVC coef_ 属性
- php - 如何通过单击使用 PHP 的链接来获取文本文件中的数据
- amazon-web-services - 将本地 Spark 作业提交给 emr
- visual-c++ - 制作循环winsock服务器
- python - 从同级目录导入文件
- javascript - Lambda 没有向 API 网关返回带有 http 响应的正文