Kevik
Kevik

Reputation: 9361

not able to serialize java object containing ArrayList using xstream library in Android

using the xstream library to serialize an object in Android into XML. however it is not working. The object shown here contains an ArrayList as a member. It is impossible to serialize objects like this with xstream? or is there some way to make this work?

http://x-stream.github.io/

 public class MainActivity extends Activity {

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.textView1);

ArrayList<String> str = new ArrayList<String>();
str.add("test xml string one");
str.add("test xml string two");
str.add("test xml string three");

ExampleObject testObj = new ExampleObject(str); 

XStream xstream = new XStream(new DomDriver());

String xmlStr = xstream.toXML(testObj);

textView.setText(xmlStr);

} // onCreate

public class ExampleObject {

ArrayList<String> memberList;

 public ExampleObject(ArrayList<String> list) {
     memberList = list;
 }

} // ExampleObject

}

Upvotes: 0

Views: 334

Answers (1)

Jake Chasan
Jake Chasan

Reputation: 6560

Did you implement the Serial UID?

Here is why it may not be functioning properly:

private static final long serialVersionUID = 12345678;

The class Serializable decodes written Objects by using the UID mentioned above. Without this number, the class will not be able to decode the object. I have also heard that if you significantly modify the class, then try to read back the data, the operation will fail.

Upvotes: 1

Related Questions