基本上我正在两侧之间进行套接字连接。一个 Java 服务器和一个 Android 客户端。我正在使用 Gson library 将对象转换为 String Json ,并通过 Android 客户端的 WriteUTF 方法发送它们。但是当我尝试在 Java 服务器中获得响应时,我得到一个“java.io.DataInputStream.readUnsignedShort(Unknown Source) 处的 java.io.EOFException”。如果我没有使用这种方法(readUnsignedShort)甚至没有发送任何 Short 怎么办?代码来了。
从 Android 客户端编写:
try {
DataInputStream in = new DataInputStream (s3.getInputStream());
DataOutputStream out = new DataOutputStream(s3.getOutputStream());
ap = (ArrayList<Produto>) InternalStorage.readObject(getBaseContext(),"pedido");
for(int i=0;i<ap.size()&& ap.size()>0;i++){
Produto p = new Produto("", "", 0f, "", "", 0);
p = ap.get(i);
Gson gson = new Gson();
String json = gson.toJson(p);
out.writeUTF(json);
out.flush();
}
并从 Java 服务器读取:
try {
// Get input from the client
DataInputStream in = new DataInputStream (server.getInputStream());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
while((line = in.readUTF()) != null) {
boolean error = false;
String json = line;
Gson gson = new Gson();
Produto p2 = gson.fromJson(json, Produto.class);
c = dbDelegate.getCommandByNumber(p2.getMesa());
p = dbDelegate.getProductByName(p2.getNome());
if(c!=null && p!=null){
error = false;
inCommand.setCommand(c);
inCommand.setDateAndTime(new Timestamp(System.currentTimeMillis()));
inCommand.setProduct(p);
inCommand.setLogin(FishingBoardFrameCashRegister.getInstance().getLogin());
inCommand.setQuantity(p2.getQuantidade());
inCommand.setPrice(String.valueOf((p2.getValor()*p2.getQuantidade())));
inCommand.setObs(p2.getObs());
inCommand.setProductName(p.getName());
inCommand.setType(Product.ALL);
inCommand.setSession(0);
inCommand.setCommandSession(c.getSession());
if(dbDelegate.insertProductInCommand(inCommand) == DbDelegate.CASH_CLOSED){
error = true;
}
FishingBoardFrameCashRegister.getInstance().updateTable();
}else{
error = true;
}
日志也可能有帮助:
trueIOException on socket listen: java.io.EOFException
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at com.keymax.server.doComms3.run(Server3.java:71)
Server3.java:71 是服务器代码中的这一行:while((line = in.readUTF()) != null) {