Question

I verified from the java docs that boolean and byte are serializable. As per the android docs, my class implements the serializable interface. I am not sure why I keep getting the exception. What am I missing here? The class is like this:

public class msgStruct implements Serializable {
    boolean pingPong = false; 
    int msgId = 0;
    byte[] bufferMsg = new byte[100];
}

This is serialized before being sent via a socket to the server, like this:

sendMsgStruct.pingPong = false;
sendMsgStruct.msgId = msgId;
rand.nextBytes(sendMsgStruct.bufferMsg);
try {
    ObjectOutputStream serializeMobile = new ObjectOutputStream(mobileSocket.getOutputStream());
    serializeMobile.writeObject(sendMsgStruct);
    serializeMobile.close();
} catch (IOException e1) {
    e1.printStackTrace();
    return false;
} 

The server deserializes like this:

try {
     ObjectInputStream deserializeServer = new ObjectInputStream(clientSocket.getInputStream());
     recvMsgStruct = (msgStruct) deserializeServer.readObject();
     deserializeServer.close();
    } catch (ClassNotFoundException e1) {
      e1.printStackTrace();
    }

I get the exception at the lines where the object is serialized and deserialized.

Était-ce utile?

La solution

Is msgStruct an inner class by chance? If so, then try to make it static, or move it to its own java file.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top