Android Question Mqtt and msgpack

giggetto71

Active Member
Licensed User
Longtime User
Hi all,
Does anyone have an example showing how to publish and receive an mqtt msgpack message?
I have seen that one custom library is available but I could not find any example. Thanks!!!
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
I have tried to use the library https://www.b4x.com/android/forum/threads/messagepack-library.88295/ by @victormedranop but cannot make it to work..
I have tried the simple code:


B4X:
Dim JSONStr As String, bb() As Byte, msgp As msgPacker

JSONStr = "{" & Chr(34) & "test" & Chr(34) & ":123}"
bb= msgp.Serialize2(JSONStr) ' bb= msgp.Serialize(JSONStr) both serialize2 and serialize fail

but both Serialize and Serialize2 methods fail throwing the below error. it almost look like the lib is missing something..I have copied both the "msgPacker.jar" and "msgPacker.xml" files in the "Additional Libraries" folder and the library shows up apparently correctly in the Libraries Manager.
any idea?

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/msgpack/MessagePack;
at msgPacker.msgPacker.Serialize2(msgPacker.java:416)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.msgpack.MessagePack" on path: DexPathList[[zip file "/data/app/b4a.example-3RqRGUJmktXM9cq1g-yXQg==/base.apk"],nativeLibraryDirectories=[/data/app/b4a.example-3RqRGUJmktXM9cq1g-yXQg==/lib/x86_64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 21 more
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 1

giggetto71

Active Member
Licensed User
Longtime User
much better! As usual..great support!
still trying to understand how the serialization works but at least it does not crash...THANKS!!!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
still trying to understand how the serialization works
This is explained in the msgpack website. Or, at least, you should be able to find interesting things if you look at a msgpack-version based on the language you are familar with.
There are a lot of implementations.
 
Last edited:
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
some progresses, but still not there... I have tried with the below code to test the lib with the test JSON sting and relevant convertion on the https://msgpack.org/index.html



B4X:
Dim JSONStr As String,bb() As Byte, bb2() As Byte,bb3(18) As Byte
    Dim UnserializedStr1,UnserializedStr2,UnserializedStr3 As String
    Dim Data As Map
  
    JSONStr = "{" & Chr(34) & "compact" & Chr(34) & ":true" & "," & Chr(34) & "schema" & Chr(34) & ":0}"
      
  
    Data.Initialize       
    Data.Put("compact",True)
    Data.Put("schema", 0)
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(Data)
    Dim JSONStr2 As String = JSONGenerator.ToString
      
  
    bb= msgp.Serialize(JSONStr)
    bb2 = msgp.Serialize2(JSONStr2)
  
    UnserializedStr1 = msgp.UnSerialize(bb)
    UnserializedStr2 = msgp.UnSerialize(bb2)
  
    bb3(0)=0x82
    bb3(1)=0xa7
    bb3(2)=0x63
    bb3(3)=0x6f
    bb3(4)=0x6d
    bb3(5)=0x70
    bb3(6)=0x61
    bb3(7)=0x63
    bb3(8)=0x74
    bb3(9)=0xc3
    bb3(10)=0xa6
    bb3(11)=0x73
    bb3(12)=0x63
    bb3(13)=0x68
    bb3(14)=0x65
    bb3(15)=0x6d
    bb3(16)=0x61
    bb3(17)=0x00
  
    UnserializedStr3 = msgp.UnSerialize(bb3)

Basically the simple:
{"compact":true,"schema":0} should be serialized as these 18 bytes 82 a7 63 6f 6d 70 61 63 74 c3 a6 73 63 68 65 6d 61 00

so I tried to define in my code that simple JSON string in 2 ways: one by using the Chr(34) and two by using the JSONGenerator, then I applied the method serialize of the lib to both and instead of getting the above 18 bytes I get:

the below 28 bytes for bb
1661014156795.png


and 29 byte for bb2

1661014259037.png


If I try to unserialize both bb and bb2 I get:
UnserializedStr1 = msgp.UnSerialize(bb) -> "{\"compact\":true,\"schema\":0}"
UnserializedStr2 = msgp.UnSerialize(bb2) -> ["{\"compact\":true,\"schema\":0}"]


As an additional test I have tried to manually compose a byte array (bb3 in the code) with the example correct arrays of the site example and when using the unserialize method on it I actually get the right JSON string: {"compact":true,"schema":0}

Does anyone have any clue ? What am I missing?
thanks!!!
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
Thank you very much. This week i will be out for vacation so i will not be able to go through your example but i will do as soon as i will get back home. Thank you very much
 
Upvote 0
Top