Android Question Send Objects to Another Computer

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I am using CSBuilder and char sequence objects to show colored text on an EditText view.

The test code I am using is:
B4X:
Dim cs As CSBuilder
Dim clst As List
 
clst.Initialize
 
cs.Initialize.Color(Colors.Red).Append("This is Red" & CRLF).PopAll
clst.Add(cs)
 
cs.Initialize.Color(Colors.Green).Append("This is Green" & CRLF).PopAll
clst.Add(cs)
 
cs.Initialize.Color(Colors.Blue).Append("This is Blue" & CRLF).PopAll
clst.Add(cs)
 
cs.Initialize.Color(Colors.DarkGray).Append(txt).PopAll
clst.Add(cs)
 
cs.Initialize
 
For Each c1 As CSBuilder In clst
  cs.Append(c1)
Next
 
edText.Text = cs

Each line of text will be created independently over time and I need the List to hold all the individual lines of text.

And it produces the following output:
upload_2018-4-13_14-23-49.png


I need to send this colored text to another device (think Erel's Walkie-Talkie example from years ago) via network sockets.

The network connection between devices is all set, but how would I transfer the List of CSBuilder objects and data defining the colored text.

I am guessing I will have to somehow create binary data representing the list of objects and send that binary data to the other device via network sockets.

On the other device I will re-create the List of CSBuilder objects.

I'm not sure how I would do this, or even where to get started.

Can someone point me in the right direction for transferring this List of CSBuilder objects representing colored text to another device?

Thanks,
Barry.
 

canalrun

Well-Known Member
Licensed User
Longtime User

Thanks, The B$XSerializer sounds like exactly what I need.

I added the following snippet to my test code (above):
Dim bta() As Byte
Dim bxs As B4XSerializator
bta = bxs.ConvertObjectToBytes(clst)

clst.Clear

clst = bxs.ConvertBytesToObject(bta)

I get an error on the ConvertObjectToBytes line that says "cannot serialize object This is Red" which I think is probably occurring when it tries to serialize the CSBuilder object that includes the text "This is Red".

Is there a way to serialize the CSBuilder object?

Thanks.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hmmm - not sure about that. Maybe CSBuilder is not a serializable object? The tutorial does say the it can serialize complex objects though.

- Colin.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Hmmm - not sure about that. Maybe CSBuilder is not a serializable object? The tutorial does say the it can serialize complex objects though.

- Colin.

I found a solution – it's not exactly pretty…

I created a CSBType that contains a color integer and a text string.

I serialize a List of objects of this Type.

I transfer the serialized List and form CSBuilder objects from the color and text values on the receiving device.

B4X:
  Dim clst As List
  clst.Initialize
 
  Dim csb As CSBTxtType
  csb.col = Colors.Red
  csb.txt = "This is Red" & CRLF
  clst.Add(csb)
 
  Dim csb As CSBTxtType
  csb.col = Colors.Green
  csb.txt = "This is Green" & CRLF
  clst.Add(csb)
 
  Dim csb As CSBTxtType
  csb.col = Colors.Blue
  csb.txt = "This is Blue" & CRLF
  clst.Add(csb)
 
  Dim csb As CSBTxtType
  csb.col = Colors.DarkGray
  csb.txt = txt
  clst.Add(csb)
 
  Dim bta() As Byte
  Dim bxs As B4XSerializator
  bta = bxs.ConvertObjectToBytes(clst)

  ' bta array is sent to other device

  ' then on other device ...
 
  Dim clst1 As List
  clst1 = bxs.ConvertBytesToObject(bta)
 
  Dim cs As CSBuilder
  cs.Initialize
 
  For Each csb As CSBTxtType In clst1
    cs.Color(csb.col).Append(csb.txt)
  Next
 
  cs.PopAll

  edText.Text = cs

This works, but I wonder if there is a better way?

Barry.
 
Last edited:
Upvote 0

KMatle

Expert
Licensed User
Longtime User
You have to check if you

- really need to send objects
- just data

Generally you should separate data from code and UI elements. What I see in you example is that you need to send just data. So don't try to serialize the CB objects. Serialize the data. Use a list and store the color information (=data) maybe via maps. A list with data can then be serialized.

Benefit: As you then just send simple data the recipient can then "decide" with which view it will be displayed (= separate UI from data). Imagine you send an EditText. A B4J app can't use it. But if you send the data, everything is fine.

Benefit2: A List with Maps can be converted to a JSON string. As it is a simple string, you can convert it to bytes and send it via AsyncStreams or just send the string to other platforms like php, etc. as every platform can handle JSON formatted strings.
 
Upvote 0
Top