0

I have a Socket server waiting for the clients send messages in a json format. I do not need to store the data into database, but for now, I would like to keep them in a hashtable. So, when the client sent a message the server will add it to the hashtable without losing the previous messages.

Here is the declaration of my hashtable:

private static Hashtable<Integer,String> table = new Hashtable<Integer,String>();

Here is the method that is called a message reach the server:

    private void HashTable(String string){
    //JsonArray questions =  new JsonArray();
    //questions = string;
    //for(int i = 0; i < questions.length(); i++) {
        //JsonObject question = questions.getJsonObject(i);
        JSONObject jsonObj;
        try {
            jsonObj = new JSONObject(string);
            int id = jsonObj.optInt("DeviceID", -1);
            String name = jsonObj.toString();
            table.put(id, name);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    //}
}

The code above is not keeping the data from the previous messages. Could anybody give a hint?

thank you

Devester
  • 1,183
  • 4
  • 14
  • 41
  • It should keep it into the hashtable....Or do you mean by keeping, that if you close and reopen the program the data is still there? – Leander Mar 05 '13 at 20:09
  • 1
    Why do you have a method called HashTable o_O – Christian Stewart Mar 05 '13 at 20:09
  • 1
    `Hashtable` is obsolete in Java. You're still using it. Is there any reason? – Lion Mar 05 '13 at 20:09
  • @Lele Thank you for your attention. I am not closing or killing the program. I am trying to see if data is been stored using multiples devices to send the message. The server is running on debug mode and I am not able to see the previous messages after sending a new one. – Devester Mar 05 '13 at 20:13
  • Hi @Lion...that structure will be changed anyway. I am using hashtable only to get it working for a demo. – Devester Mar 05 '13 at 20:14
  • @it_farway try to `System.out.println(table);` just right after the `put` method - is the value there? If so is the static `table` the same istance of `Hashtable` that the `table` in `HashTable` class' constructor? (check it like this: System.out(`StaticTableHolder.table == table);` - make the static field public: just for the test) – Xeon Mar 05 '13 at 20:18

2 Answers2

2

int id = 1; - you never change this, so all you are doing is replacing the entry for ID 1 each time. You need either a unique ID generator or just to increment id every time an entry is added.

Edit: As the commenters rightly point out, you should be using HashMap not HashTable, and you really shouldn't be calling your method Hashtable...

Edit 2: Well you changed the code sample so this is no longer accurate, but now what's happening is either DeviceID doesn't exist so you get -1 or you keep getting the same device ID. Of course, you say you want to store multiple messages from the same device, so using a device ID on it's own isn't what you want to do. You should probably have some sort of message ID instead, most likely generated on the device and somehow incorporating the device ID to help ensure it's unique.

jrtc27
  • 8,496
  • 3
  • 36
  • 68
1

Thats because , when the "DeviceId" is null from the Json , you will always end up storing with the same key "1". Hence it replaces the old value.

Solution

private static AtomicInteger count = new AtomicInteger ;

in the place where you insert into hashtable adapt to

int id = jsonObj.optInt("DeviceID", count.addAndGet(1) );
Sudhakar
  • 4,823
  • 2
  • 35
  • 42