I'm trying to serialize and deserialize an array list with a object inside:
HairBirt param = new HairBirt();
param.setName("name");
param.setValue(2.3f);
HairBirt param2 = new HairBirt();
param2.setName("name2");
param2.setValue(2.4f);
ArrayList<HairBirt> list = new ArrayList<HairBirt>();
list.add(param);
list.add(param2);
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
ObjectOutputStream obj_out = new ObjectOutputStream(bos);
obj_out.writeObject(list);
} catch (IOException e) {
e.printStackTrace();
}
String encoded = bos.toString();
try {
encoded = URLEncoder.encode(encoded, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
System.out.print("The serialized output is: " + encoded);
//DECODE
ArrayList<HairBirt> paramDecoded;
String myParam = null;
try {
myParam = URLDecoder.decode(encoded, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
System.out.println("Got parameters");
ByteArrayInputStream bis = new ByteArrayInputStream(myParam.getBytes());
try {
ObjectInputStream obj_in = new ObjectInputStream(bis);
paramDecoded = (ArrayList<HairBirt>) obj_in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
The HairList object is also a serializable object.
This code execution is returning the following error:
java.io.InvalidClassException: java.util.ArrayList; local class incompatible: stream classdesc serialVersionUID = 8664875232659988799, local class serialVersionUID = 8683452581122892189
in line paramDecoded = (ArrayList<HairBirt>) obj_in.readObject();
I don't know what i'm doing wrong. Can you give a tip?
Update:
Resolved: Just used a native array of HairBirt instead of a ArrayList and it works:
HairBirt[] list = new HairBirt[x];
instead of
ArrayList<HairBirt> list = new ArrayList<HairBirt>();
Thank you all for your help.