B4J Question Decoding Payload from MQTT server

JMB

Active Member
Licensed User
Longtime User
Hi there.

I am using an ESP8266 board to transmit a message to my CloudMQTT account which is then viewed using B4J.

I have successfully transmitted the message and it is being received ok, but I am having problems decoding it:

I encode my message as follows on the ESP board in B4R.

Type MyMessage (messageInfo As String, messageTime As ULong)

The code that sends the message from the ESP8266 board is as follows: -

Dim myMessage As MyMessage
If wifi.IsConnected Then

myMessage.messageInfo = "Time here is:"
myMessage.messageTime = Millis
Log("Sending...Time here is: ", Millis)
mqttClient.Publish("esp", ser.ConvertArrayToBytes(Array(myMessage)))
End If

This all works fine.

The code in my B4J app that receives messages is as follows:

Type MyMessage (messageInfo As String, messageTime As Long)

Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte

Dim myMessage As MyMessage
Log("Message arrived")
Log(BytesToString(Payload, 0, Payload.Length, "utf8"))
'Show the message as it is raw
lblESPInfo.Text = BytesToString(Payload, 0, password.Length, "utf8")

Dim obj As Object = serializator.ConvertBytesToObject(Payload)

If obj Is MyMessage Then

myMessage = obj
Log(myMessage.messageInfo + myMessage.messageTime)
End If
End Sub

The program crashes at the line:

Dim obj As Object = serializator.ConvertBytesToObject(Payload)

with

java.util.zip.ZipException: incorrect header check

How can I convert the payload back to the type of variable that I know I sent?

Thanks for any help.

JMB
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use [code]code here...[/code] tags when posting code.

1. You must use B4RSerializator on both B4R and B4J. Don't confuse it with B4XSerializator.
https://www.b4x.com/android/forum/t...ceive-objects-instead-of-bytes.72404/#content

2. B4RSerializator doesn't support custom types. You will need to change the code to:
B4X:
mqttClient.Publish("esp", ser.ConvertArrayToBytes(Array(myMessage.messageInfo, myMessage.messageTime)))
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…