我的xml文件的unmarshaller有问题。我写了客户端和服务器程序: 客户端:
public void run(){
try(Socket skt = new Socket(host,port);
PrintStream Output = new PrintStream(skt.getOutputStream());
BufferedReader Input = new BufferedReader(new InputStreamReader(skt.getInputStream())))
{
Output.println("NAME");
String buf=Input.readLine();
app.setNameLbl(buf);
Output.println("RAMTOTAL");
buf=Input.readLine();
ramTotal=Integer.parseInt(buf);
app.getRamTotalLbl().setText(buf+" MB");
app.getRamBar().setMaximum(ramTotal);
Status status;
Output.println("STATUS");
JAXBContext jaxbContext = JAXBContext.newInstance(Status.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
status =(Status) jaxbUnmarshaller.unmarshal(Input);//<--- here
System.out.println(status.getTemp());
while(status!=null){
app.getRamBar().setValue((int)((ramTotal-status.getRamAvailable())*100)/1024);
app.getTempBar().setValue((int)Math.round(status.getTemp()));
app.getTempBar().setString(String.valueOf(status.getTemp())+"'C");
StringBuilder sb = new StringBuilder();
for(String x : status.getPplLogged()){
sb.append(x);
sb.append("\n");
}
app.getLoggedUsersArea().setText(sb.toString());
Thread.sleep(2000);
Output.println("STATUS");
status = (Status) jaxbUnmarshaller.unmarshal( Input );
}
}
服务器:
public ResponseThread(Socket socket) {
super("ResponseThread");
this.socket = socket;
}
public void run() {
try(BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true))
{
String inputLine ;
while ((inputLine = in.readLine()) != null) {
if(inputLine.equals("STATUS")){
JAXBContext jaxbContext = JAXBContext.newInstance(Status.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
status.update();
StringWriter dataWriter = new StringWriter();
jaxbMarshaller.marshal(status, dataWriter);
out.print(dataWriter.toString());
out.println();
out.flush();
}
else if(inputLine.equals("NAME")){
out.println(StaticActions.getName());
}
else if(inputLine.equals("RAMTOTAL")){
out.println(StaticActions.getRamTotal());
}
else if(inputLine.equals("RESET")){
StaticActions.rebootPi();
}
else if(inputLine.equals("TEST")){
out.println("OK");
}
}
socket.close();
} catch (IOException | JAXBException e) {
e.printStackTrace();
}
}
主要问题是它卡在我标记的位置。我认为它正在等待EOF标记。但我不知道如何通过它。 请帮我解决这个问题。 祝你有愉快的一天!