0

I am trying to add a custom array to shared preferences in android.

I have a custom arrayList that contains set data (Strings and NodeList), the data is stored in an array with the following name ArrayList<XMLDataArrayStructure> menuItems = new ArrayList<XMLDataArrayStructure>(); I have found a solution that says I should use an "ObjectSerializer" class to store the data. Everything works fine up until a point, I get the following error

W/System.err(32052): java.io.NotSerializableException: package.XMLDataArrayStructure
W/System.err(32052):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
W/System.err(32052):    at java.util.ArrayList.writeObject(ArrayList.java:644)
W/System.err(32052):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(32052):    at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err(32052):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
W/System.err(32052):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
W/System.err(32052):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
W/System.err(32052):    at package.ObjectSerializer.serialize(ObjectSerializer.java:17)
W/System.err(32052):    at package.GatherXMLData.getData(GatherXMLData.java:94)
W/System.err(32052):    at package.Splash$gatherData.doInBackground(Splash.java:63)
W/System.err(32052):    at package.Splash$gatherData.doInBackground(Splash.java:1)
W/System.err(32052):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
W/System.err(32052):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
W/System.err(32052):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
W/System.err(32052):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W/System.err(32052):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err(32052):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err(32052):    at java.lang.Thread.run(Thread.java:864)

It is failing on the following

public static String serialize(Serializable obj) throws IOException {
    if (obj == null)
        return "";
    ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
    ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
    objStream.writeObject(obj);
    objStream.close();
    return encodeBytes(serialObj.toByteArray());
}

Can someone help please, this would really help me a lot as I would only have to make one network call then.

Thanks

EDIT

Here is the code from the XMLDataArrayStructure class, it does implement serializable but I am getting the following error still

CODE

public class XMLDataArrayStructure implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    String title, date, start_time, end_time, venue, image, sponsor_logo;
    NodeList people;
    Spanned description;

    public void setPeople(NodeList people) {
        this.people = people;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setStartTime(String start_time) {
        this.start_time = start_time;
    }

    public void setEndTime(String end_time) {
        this.end_time = end_time;
    }

    public void setVenue(String venue) {
        this.venue = venue;
    }

    public void setDescription(Spanned spanned) {
        this.description = spanned;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public void setSponsorLogo(String sponsor_logo) {
        this.sponsor_logo = sponsor_logo;
    }

    public NodeList getPeople() {
        return people;
    }

    public String getTitle() {
        return title;
    }

    public String getDate() {
        return date;
    }

    public String getStartTime() {
        return start_time;
    }

    public String getEndTime() {
        return end_time;
    }

    public String getVenue() {
        return venue;
    }

    public Spanned getDescription() {
        return description;
    }

    public String getImage() {
        return image;
    }

    public String getSponsorLogo() {
        return sponsor_logo;
    }

}

ERROR

W/System.err(1380): java.io.NotSerializableException: android.text.SpannableStringBuilder
W/System.err(1380):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
W/System.err(1380):     at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
W/System.err(1380):     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
W/System.err(1380):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
W/System.err(1380):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
W/System.err(1380):     at java.util.ArrayList.writeObject(ArrayList.java:644)
W/System.err(1380):     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(1380):     at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err(1380):     at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
W/System.err(1380):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
W/System.err(1380):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
W/System.err(1380):     at package.ObjectSerializer.serialize(ObjectSerializer.java:17)
W/System.err(1380):     at package.GatherXMLData.getData(GatherXMLData.java:89)
W/System.err(1380):     at package.Splash$gatherData.doInBackground(Splash.java:63)
W/System.err(1380):     at package.Splash$gatherData.doInBackground(Splash.java:1)
W/System.err(1380):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
W/System.err(1380):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
W/System.err(1380):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
W/System.err(1380):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W/System.err(1380):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err(1380):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err(1380):     at java.lang.Thread.run(Thread.java:864)
0

2 Answers 2

1
Class XMLDataArrayStructure implements Serializable
Sign up to request clarification or add additional context in comments.

1 Comment

Do you have the documentation for that class ? I believed it is a custom class used by the OP.
0

In all likelihood , your class XMLDataArrayStructure is non-Serializable . There can be two reasons :

  1. Your class itself doesn't implement Serializable.

  2. Even if the class implements the Serializable interface contains statements that refers to an non-Serializable object.

Please post the code of the class in case you can .

4 Comments

What is NodeList and Spanned ? Are they Serializable ?
NodeList contains a dataset of xml data and Spanned is a html dataset. How would I know if they are Serializable?
Do you have the definition or any documentation for those two classes ?
I don't but I have taken them out any the function now works. I am going to have to figure out a different way of storing that data set. Thanks for the help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.