-1

I have got a server that is connected to an SQLite Database. From here I'm putting the data into a HashMap and then into Json to send over the a client handler.

I'm then trying to to use a swing client GUI to put the data from the server into a table. On the client side to make sure I'm receiving the data I'm printing the reply to the console.

while (true){
            count++;

            if (socket == null){
                guiClientSays("Waiting for connection to be reset...");
                synchronized (waitObject){
                    try{
                        waitObject.wait();
                    }catch (InterruptedException ex){
                        Logger.getLogger(GUIClient.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            guiClientSays("Waiting for message" + count + " from server...");
            String reply = null;
            try{
                reply = bufferedReader.readLine();
                guiClientSays("Received \"" + reply + "\" from server.");
            }catch (IOException ex){
                guiClientSays("IOException " + ex);
            }
}

In the console I'm getting this

Client1: Received "{"1":{"driverId":1,"driverRef":"hamilton","number":"44","code":"HAM","forename":"Lewis","surname":"Hamilton","dob":"07/01/1985","nationality":"British","url":"http://en.wikipedia.org/wiki/Lewis_Hamilton"},"2":{"driverId":2,"driverRef":"heidfeld","number":"","code":"HEI","forename":"Nick","surname":"Heidfeld","dob":"10/05/1977","nationality":"German","url":"http://en.wikipedia.org/wiki/Nick_Heidfeld"},"3":{"driverId":3,"driverRef":"rosberg","number":"6","code":"ROS","forename":"Nico","surname":"Rosberg","dob":"27/06/1985","nationality":"German","url":"http://en.wikipedia.org/wiki/Nico_Rosberg"}, etc

To make sure I could populate the table I connected the GUI client to the database that worked with no issues

ArrayList<Driver> drivers = Driver.readAllDrivers();
        DefaultTableModel model = (DefaultTableModel) driverTable.getModel();

        model.setRowCount(0);
        for (Driver driver : drivers){
            model.addRow(new Object[]{
                    driver.getDriverId(),
                    driver.getDriverRef(),
                    driver.getNumber(),
                    driver.getCode(),
                    driver.getForename(),
                    driver.getSurname(),
                    driver.getDob(),
                    driver.getNationality(),
                    driver.getUrl()
            });
        }

driverTable.setModel(new DefaultTableModel(
                null,
                new String[]{"Driver ID", "Driver Ref", "Number", "Code", "Forename", "Surname", "DOB", "Nationality"
                        , "URL"}
        ));

Once I had got the data to be sent by the server I tried this:

String driverReply;
            driverReply = reply;



            String rows[] = ((String) driverReply).split("\n");
            Vector<Vector<String>> dataVector = new Vector<Vector<String>>();
            for (String row : rows) {
                row = row.trim();
                Vector<String> data = new Vector<String>();
                data.addAll(Arrays.asList(row.split(",")));
                dataVector.add(data);
            }
            Vector<String> header = new Vector<String>(9);
            header.add("Driver ID");
            header.add("Driver Ref");
            header.add("Number");
            header.add("Code");
            header.add("Forename");
            header.add("Surname");
            header.add("DOB");
            header.add("Nationality");
            header.add("URL");

The image shows the gui and how it outputs the table

GUI

I should be getting 800 rows and it should display like this:

1 hamilton 44 HAM Lewis Hamilton 07/01/1985 British http://en.wikipedia.org/wiki/Lewis_Hamilton 2 heidfeld HEI Nick Heidfeld 10/05/1977 German http://en.wikipedia.org/wiki/Nick_Heidfeld

etc

stef_axel
  • 1
  • 1

1 Answers1

0

You can use JsonObject instead String type for var "reply".

https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html

In the client side, you can write:

            JSONParser parser = new JSONParser();

            JSONObject reply = (JSONObject) parser.parse(bufferedReader.readLine());

After that, you can set DataVector with all reply values, as follows:

            Vector<Vector<String>> dataVector = new Vector<Vector<String>>();
            reply.keySet().forEach(rowKeyStr ->
            {
               JSONObject row = (JSONObject)jsonObj.get(rowKeyStr);
               Vector<String> data = new Vector<String>();
               row.keySet().forEach(fieldKeyStr ->
               {
                  String field = (String)jsonObj.get(fieldKeyStr);
                  data.add(field);
               }
               dataVector.add(data);
            });

I hope this help you.

Isma
  • 168
  • 3
  • 9
  • Thank you for the reply. On the client handler I'm using driverJSON = gson.toJsonTree(driverHashMap).getAsJsonObject(); I'm then overriding a run method inside which String driverLookup = driverJSON.toString(); That solution should still work. Also I'm using gson-2.9.0.jar and JsonParser parser = new JsonParser(); has been deprecated. Is there a work around this? – stef_axel Mar 31 '22 at 18:14
  • I believe you can see different ways to avoid this issue in this question: https://stackoverflow.com/questions/60771386/jsonparser-is-deprecated – Isma Mar 31 '22 at 19:04
  • Thank you once again and thank you for the help. One final question JSONObject row = (JSONObject)jsonObj.get(rowKeyStr); and String field = (String)jsonObj.get(fieldKeyStr); is jsonObj being declared as JSONObject = new JSONObject or am I missing something else. I'm new to java and using JSON seemed be the best way I could find of implementing what I want to do. – stef_axel Mar 31 '22 at 21:49
  • Yes, when you add (JSONObject) or (String) between parentheses you are making a type casting (converting data from one data type to another data type). – Isma Apr 01 '22 at 09:28