我试图使用网络库kryonet创建一个多人游戏,我得到了连接并发送字符串工作但是,现在我试图发送对象。我想要做的是发送一个数组列表,但它给了我这个错误。我也尝试只发送一个对象,它给了我同样的错误。
Exception in thread "Client" com.esotericsoftware.kryonet.KryoNetException: Error during deserialization.
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:141)
at com.esotericsoftware.kryonet.Client.update(Client.java:247)
at com.esotericsoftware.kryonet.Client.run(Client.java:333)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): com.connorbrezinsky.spaceraiders.objects.Metor
at com.esotericsoftware.kryo.Kryo.newInstantiator(Kryo.java:1048)
at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1060)
at com.esotericsoftware.kryo.serializers.FieldSerializer.create(FieldSerializer.java:228)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:217)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:109)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:18)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:58)
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:139)
... 3 more
我尝试在Metor类中创建一个没有args但没有工作的构造函数
Exception in thread "Client" com.esotericsoftware.kryonet.KryoNetException: Error during deserialization.
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:141)
at com.esotericsoftware.kryonet.Client.update(Client.java:247)
at com.esotericsoftware.kryonet.Client.run(Client.java:333)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.esotericsoftware.kryo.KryoException: java.lang.IllegalArgumentException: Can not set org.newdawn.slick.geom.Shape field com.connorbrezinsky.spaceraiders.objects.Object.s to java.lang.Float
Serialization trace:
s (com.connorbrezinsky.spaceraiders.objects.Metor)
metors (com.connorbrezinsky.spaceraiders.main.Response)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:626)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:221)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:109)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:18)
at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:654)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:605)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:221)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:735)
at com.esotericsoftware.kryonet.KryoSerialization.read(KryoSerialization.java:58)
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:139)
... 3 more
Caused by: java.lang.IllegalArgumentException: Can not set org.newdawn.slick.geom.Shape field com.connorbrezinsky.spaceraiders.objects.Object.s to java.lang.Float
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.read(FieldSerializer.java:619)
... 13 more
继承客户端代码
Kryo kryo = client.getKryo();
kryo.register(Request.class);
kryo.register(Response.class);
kryo.register(java.util.ArrayList.class);
kryo.register(Metor.class);
client.start();
try {
client.connect(10000, "192.168.1.130", 54555);
} catch (Exception er) {
er.printStackTrace();
}
Request request = new Request();
request.text="map_metor";
client.sendTCP(request);
client.addListener(new Listener() {
public void received(Connection connection, Object object) {
Response response = (Response) object;
System.out.println(response.metors);
}
});
继承服务器代码
public static Server server;
public static World world;
public static final int PORT = 54555;
public static void main(String[] args) throws IOException {
System.out.println("Starting server...");
server = new Server();
world = new World(2000,2000);
Kryo kryo = server.getKryo();
kryo.register(Request.class);
kryo.register(Response.class);
kryo.register(java.util.ArrayList.class);
kryo.register(Metor.class);
server.start();
server.bind(54555);
System.out.println("Server started, generating world");
world.setMaxMetors(20);
world.setMaxPlanets(10);
world.generateObjects();
server.addListener(new Listener() {
public void received(Connection connection, Object object) {
if (object instanceof Request) {
Request request = (Request) object;
System.out.println(request.text);
if (request.text.equalsIgnoreCase("map_metor")) {
Response response = new Response();
response.metors = world.metors;
connection.sendTCP(response);
}else if(request.text.equalsIgnoreCase("map_planets")) {
Response response = new Response();
response.planets = world.planets;
connection.sendTCP(response);
}
}
}
});
}
对于长篇文章感到抱歉,但这似乎是一个奇怪的问题,除非我完全忘记某些事情。
修改 所以我得出结论Kryonet有问题序列化slick2d Image类,或者与Kryonet& amp; slick2d。
答案 0 :(得分:0)
你必须确保在你使用kryonet发送的类中拥有所有相同的变量。