我已经尝试了好几天,更改了很多东西,但都没有效果。
当我试图使用我的三星设备调试客户端时,在Android Studio中出现以下错误:
com.esotericsoftware.kryonet.KryoNetException: Incorrect number of bytes (1 remaining) used to deserialize object: null
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:146)
at com.esotericsoftware.kryonet.Client.update(Client.java:255)
at com.esotericsoftware.kryonet.Client.run(Client.java:338)
at java.lang.Thread.run(Thread.java:856)
我认为它使用的是相同的Java版本,并且我完全按照相同的方式注册了该类。 客户端:
Client client = new Client();
Kryo kryo = client.getKryo();
kryo.register(SomeRequest.class);
kryo.register(SomeResponse.class);
client.start();
try{
client.connect(5000, "10.0.0.4", 31055, 32055);
}catch (IOException e) {
throw new GdxRuntimeException(e);
}
SomeRequest request = new SomeRequest();
request.text = "Here is the request";
client.sendTCP(request);
client.addListener(new Listener() {
public void received (Connection connection, Object object) {
if (object instanceof SomeResponse) {
SomeResponse response = (SomeResponse)object;
System.out.println(response.text);
}
}
});
服务器端:
Server server = new Server();
Kryo kryo = server.getKryo();
kryo.register(SomeRequest.class);
kryo.register(SomeResponse.class);
server.start();
System.out.println("server started");
server.bind(31055, 32055);
server.addListener(new Listener(){
public void connected(Connection connection){
System.out.println("connect");
}
public void received (Connection connection, Object object) {
if (object instanceof SomeRequest) {
SomeRequest request = (SomeRequest)object;
System.out.println(request.text);
SomeResponse response = new SomeResponse();
response.text = "Thanks";
connection.sendTCP(response);
}
}
});
}
类:
class SomeRequest{
public String text;
}
class SomeResponse {
public String text;
}
从代码可以看出,它非常基础,但是连接起来却是一场噩梦。 请帮忙!! 我正在Android Studio中创建客户端,并在Eclipse中创建服务器。这可能是问题的原因吗?这是我能想到的唯一合理的原因。 如何解决?