首页 > 解决方案 > 从 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 的元素之一。如何将此响应转换为表格?

标签: javarestjtabledesktop-application

解决方案


推荐阅读