0

So i am working on a java desktop application and what i am looking for is information on how i am going to retrieve information into a jtable from the RESTful API i am using.I have searched through but i did not find anything related to my problem yet. Can i have some guidance on this in order to help me solve my problem? Of course i don't want to be given the solution i just want some feedback on where and what to search for. Thanks! Here's what i have implemented until now

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);

        }

    }

This is what i have right now, i get the JSON response from the API and for now i print one of the elements of the JSON. How can i convert this response into a table?

  • 1
    So you have data from database? How are you displaying what kind of collection you are using for it(these are not related to question and irreverent). Hope you know how to consume [REST END POINT](https://stackoverflow.com/questions/12916169/how-to-consume-rest-in-java) and then convert string to you object just like database record conversion you did earlier. – mallikarjun Mar 04 '19 at 12:53
  • Please see my updated question –  Mar 04 '19 at 13:00
  • 1
    Do you get the data for the field? If yes [convert it to java object](http://www.java67.com/2016/10/3-ways-to-convert-string-to-json-object-in-java.html) and return the data to the view. – mallikarjun Mar 04 '19 at 13:04
  • i know how to convert to object but my problem is on how i insert the data into the jtable –  Mar 04 '19 at 13:06

0 Answers0