我试图序列化和反序列化内部对象的数组列表:
HairBirt param = new HairBirt();
param.setName("name");
param.setValue(2.3f);
HairBirt param2 = new HairBirt();
param2.setName("name2");
param2.setValue(2.4f);
ArrayList list = new ArrayList();
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 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) obj_in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
HairList对象也是一个可序列化的对象。
此代码执行返回以下错误:
java.io.InvalidClassException: java.util.ArrayList; local class
incompatible: stream classdesc serialVersionUID = 8664875232659988799, local
class serialVersionUID = 8683452581122892189
排队 paramDecoded = (ArrayList) obj_in.readObject();
我不知道我在做什么错。你能给个小费吗?
更新:
解决: 仅使用HairBirt的本机数组而不是ArrayList即可工作:
HairBirt[] list = new HairBirt[x];
代替
ArrayList list = new ArrayList();
感谢大家的帮助。