所以我试图将一个Object从客户端发送到服务器,然后我用这个对象做一些事情。我确认在发送时对象包含数据。当我在调试器中读取我的对象时,它的所有属性都是null。
以下是我发送对象的地方:
private class registerUser extends Task<Void>
{
Users user;
private static final String code = ".register";
private static final String host = "localhost";
private static final int portNumber = 4444;
private clientThread clientThread;
public registerUser(Users user)
{
this.user = user;
}
@Override
protected Void call() throws Exception
{
try
{
Socket socket = new Socket(host, portNumber);
Thread.sleep(1000);
//Setup I/O
ObjectOutputStream outToServerObject = new ObjectOutputStream(socket.getOutputStream());
//PrintWriter serverOutString = new PrintWriter(socket.getOutputStream(), false);
InputStream serverInString = socket.getInputStream();
//serverOutString.println(code);
// serverOutString.flush();
outToServerObject.writeObject(user);
outToServerObject.flush();
}
catch (IOException e)
{
System.err.println("Fatal Connection error!");
e.printStackTrace();
}
return null;
}
当我按下GUI中的按钮时,将使用上述任务,如下所示:
public void btn_registerUser(ActionEvent actionEvent) throws IOException
{
user.setUserName(txt_Username.getText());
user.setFirstName(txt_FirstName.getText());
user.setLastName(txt_LastName.getText());
user.setCity(txt_City.getText());
user.setBirthday(date_Birthday.getValue());
Task<Void> task = new registerUser(user);
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}
收到对象的服务器中的代码:
//Setup I/O
this.clientOut = new PrintWriter(socket.getOutputStream(), false);
ObjectInputStream inFromClientObject = new ObjectInputStream(socket.getInputStream());
// Scanner in = new Scanner(socket.getInputStream());
Users user = (Users) inFromClientObject.readObject();
doSomething(user);
User.java类,它是辩论中的对象
public class Users implements Serializable
{
/*
Variables
TODO Create a friend list
*/
private transient IntegerProperty userID;
private transient StringProperty userName;
private transient StringProperty firstName;
private transient StringProperty lastName;
private transient StringProperty city;
private transient ObjectProperty<LocalDate> birthday;
private transient ListProperty<String> musicGenre;
private transient List<String> musicGenres = new ArrayList<>();
/**
* Default constructor.
*/
public Users()
{
this(null,null,null,null,null);
}
/**
* Constructor with some initial data.
* Takes in a {@code String} and sets a reference
*
* @param userName
*
*/
public Users(String userName, String firstName, String lastName, String city, LocalDate birthday)
{
this.userName = new SimpleStringProperty(userName);
this.userID = new SimpleIntegerProperty();
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.city = new SimpleStringProperty(city);
this.birthday = new SimpleObjectProperty<LocalDate>(birthday);
ObservableList<String> observableList = FXCollections.observableArrayList(this.musicGenres);
this.musicGenre = new SimpleListProperty<String>(observableList);
}
}
由于这个问题已经很久了,我已经把吸气剂和制定者排除在外了
因此,在运行服务器的调试器中,当我doSomething(user)
*修改 这些建议正在制定中,但我遇到了问题
private List<String> musicGenreList = new ArrayList<>();
ObservableList<String> observableList = FXCollections.observableArrayList(musicGenreList);
private ListProperty<String> musicGenre = new SimpleListProperty<String>(observableList);
这是我可以向ListProperty
添加项目的唯一方法,但observableList
无法序列化
答案 0 :(得分:1)
像James_D一样,基本上我必须创建自定义WriteExternal
和ReadExternal
在Users.java
中需要进行更改
public class Users implements Externalizable
{
public Users(String userName, String firstName, String lastName, String city, LocalDate birthday)
{
setUserName(userName);
setFirstName(firstName);
setBirthday(birthday);
}
private IntegerProperty userID = new SimpleIntegerProperty(this, "id");
private StringProperty userName = new SimpleStringProperty(this, "userName');
private ObjectProperty<LocalDate> birthday = new SimpleObjectProperty<>(this, "birthday");
//Create setters and getters...
@Override
public void writeExternal(ObjectOutput out) throws IOException
{
out.writeInt(getUserID());
out.writeObject(getUserName());
out.writeObject(getBirthday());
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
setUserID((int) in.readInteger());
setUserName((String) in.readObject());
setBirthday((LocalDate) in.readObject());
}
}
<强>然而强> 我还没有能够使用以下方法序列化ListProperty
private ObservableList<String> observableList = FXCollections.observableArrayList();
private ListProperty<String> newList = new SimpleListProperty<>(this, "musicGenre", observableList);
根据我的需要,需要以这种方式实例化ListProperty,因为我需要使用user.newListProperty().get().add("String")
向其添加String项而不使用ObservableList
我无法执行此操作
如果有人知道如何解决这个问题,我们将不胜感激