我试图使用网络库kryonet创建一个多人游戏,我获得了连接并发送了字符串,但是现在我试图发送对象。我想做的是发送一个数组列表,但是它给了我这个错误。我还尝试仅发送1个对象,但它给了我同样的错误。

 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);
                }
            }
        }
    });



}


很长的帖子很抱歉,但是除非Im完全不注意,否则这似乎是一个奇怪的问题。

编辑:
因此,我得出的结论是,Kryonet在序列化slick2d Image类时遇到问题,或者与Kryonet&slick2d存在不同的冲突。

最佳答案

您必须确保在使用kryonet发送的类中具有所有相同的变量。

10-07 20:59