Search code examples
javaobjectinputstream

ObjectOutputStream in Java


I have a function

public void loadUserOnline() {
        try {
            oos.writeObject(req); //Send request to Server to get online users
            oos.flush();
            LinkedList<UserOnlineInfo> userOnlineInfoList = (LinkedList<UserOnlineInfo>)ois.readObject(); // read object from Server contains online users
            Vector<String> listData = new Vector<>(); // a Vector for JList
            for (int i = 0; i < userOnlineInfoList.size(); i++) {
                listData.add(userOnlineInfoList.get(i).getUser() + " --- " + userOnlineInfoList.get(i).getStatus()); // add elements to Vector
            }
            theList.setListData(listData); // set data source for JList
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

The first time I call this function, it gets data from a server. Then data from the server changes. I call this function a second time, and the data is the same as the first time. Why?


Solution

  • You need to call ObjectOutputStream.reset() every time you want to resend the same object with new values, or else use writeUnshared(). See the Javadoc.