2

I want to send an object using Messageapi from a smart watch to smart phone. Since messageapi needs byte array to send messages, I want to convert my object to a byte array. I used apache utils to serialize and deserialize. I am getting the following error while serializing:

org.apache.commons.lang3.SerializationException: java.io.NotSerializableException: android.os.Bundle
           at org.apache.commons.lang3.SerializationUtils.serialize(SerializationUtils.java:156)
           at org.apache.commons.lang3.SerializationUtils.serialize(SerializationUtils.java:178)

My object has two variables - one string and one android bundle.

Following is the code implementation:

To Serialize:

byte[] data = SerializationUtils.serialize(myObject);

To deserialize:

MyObject myObject = (MyObject) SerializationUtils.deserialize(byte[] data)

What is the best method to serialize an object containing android bundle?

MyObject class is as below:

public class MyObject implements Serializable {

    private String type;
    private Bundle data;

    public String getType() {
        return type;
    }

    public void setType(String value) {
        type = value;
    }

    public Bundle getData() {
        return data;
    }

    public void setData(Bundle value) {
        data = value;
    }

}

PS: I have tried Parcelable to serialize objects containing android bundle and it works in devices of same OS versions. But it doesn't work when the two devices involved have different versions of OS(for example, sending a message from API level 23 device to an API level 15 device using wifip2p). So I am looking for a method which works all the time.

NewOne
  • 401
  • 5
  • 19

1 Answers1

2

Problem:

I think some object are there that can not be Serialized like Bundle and Context. The cause of error it self is saying cannot serialize Bundle.

java.io.NotSerializableException: android.os.Bundle

Solution :

Try removing private Bundle data; from MyObject and see if you are getting the same error ??

You need to find a way of serializing Bundle. Below is a question you can refer :

How to serialize a Bundle?

Community
  • 1
  • 1
Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
  • I have already tried it. It works. Parcelable can do it but only across devices of same OS. In the link you have given, is there any solution which will help me to send and receive data across devices? I need to serialize data in one device, send, receive in another device and deserialize it to retrieve the data. – NewOne Jun 20 '16 at 06:15
  • You can use any other format then `Bundle`. Use JSON it is very good and trending now a days..!! – Janki Gadhiya Jun 20 '16 at 06:18
  • Thanks. So you are saying I can convert an object to byte array and reverse the conversion using JSON. Let me try. – NewOne Jun 20 '16 at 06:23
  • Hi, could you help me with few lines of code? I need to convert MyObject into byte array and reverse it at the other end. – NewOne Jun 20 '16 at 07:09
  • this may help you : http://stackoverflow.com/questions/7947871/convert-a-string-to-a-byte-array-and-then-back-to-the-original-string – Janki Gadhiya Jun 20 '16 at 07:11
  • If you find my answer helpful then please see this : http://meta.stackexchange.com/a/5235 & mark the answer as correct..!! – Janki Gadhiya Jun 20 '16 at 07:12
  • I tried JSON and I got TransactionTooLargeException. Could you help me with some lines of code that actually serializes an object containing android bundle? – NewOne Jun 21 '16 at 03:01
  • android bundle cannot be serialized..!! that's what i have answered i have told you to try an alternative.. Something other then bundle..!! – Janki Gadhiya Jun 21 '16 at 03:42
  • please accept the answer if it was helpful see here : http://meta.stackexchange.com/a/5235/333378 – Janki Gadhiya Jul 01 '16 at 05:56