B4R Question issue to extract data from html get

candide

Active Member
Licensed User
hi
i try to extract data from html interface after a get, but my loop is not working
at end of inputs extract, , i want to convert inputs to byte with sr.convertarraytobytes to store after in eeprom but,
- if i store inputs in an array as string (init(14) as string), extraction is correct but sr.convertarraytobytes is not possible
- if i use init(14) as object, extraction of inputs is not correct but sr.convertarraytobytes seems possible.
i am not able to find correct way to make this extract and to send it to eeprom, your help will be welcome

i create this small code to test my issue:
B4X:
sub Process_Globals
    Public Serial1 As Serial
    Private bc As ByteConverter
    Private sr As B4RSerializator
    Private htmlinput() As Byte ="GET /save?_nwk=Livebox-toto&_key=123456123456123456&_mth=my.cloudmqtt.com&_mtp=10753&_mtc=rfgate_070255&_mtu=uver750&_mtk=azertytyu&_ms1=tele%2Frfbridge%2FRESULT&_ms2=cmnd%2Frfgate&_ms3=stat%2Frfbridge%2FRESULT&_mt1=cmnd%2Frfbridge%2Fbacklog&_mt2=tele%2Frfgate%2FRESULT&_ss1=4DDDDD&_ss2=CEEEEE&_save= HTTP/1.1"
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Astream_NewData(htmlinput)
End Sub

Private Sub Astream_NewData (Buffer() As Byte)
    Log("startB=",Buffer,"=endB")
        
      If bc.IndexOf(Buffer, "save?_nwk=") <> -1 Then
        Dim init(14) As String        'object NOK
         Dim pt1,pt2 As Int
        pt1 = bc.IndexOf(Buffer, "save?_nwk=")+10
        Dim nb As Int = 0
        Dim nkay() As String = Array As String("&_key=","&_mth=","&_mtp=","&_mtc=","&_mtu=","&_mtk=","&_ms1=","&_ms2=","&_ms3=","&_mt1=","&_mt2","&_ss1","&_ss2","&_save=")
        For Each n As String In nkay
            pt2 = bc.IndexOf(Buffer, n)
            Log("pt2=",pt2)
            init(nb) =bc.StringFromBytes(bc.substring2(Buffer,pt1,pt2))
            pt1 = pt2+6        
            nb = nb +1
        Next
    Log("inputs from html !")    
    For Each o As Object In init
    Log("html=",o)
    Next
'        Log("htmldata:",sr.convertarraytobytes(init))            'with init() as string NOK 
'        Main.SaveNetworkDetails(sr.ConvertArrayToBytes(init))

        Log("memory=", AvailableRAM)
        Log("stack :",StackBufferUsage)

    End If
    End Sub
 

candide

Active Member
Licensed User
in fact i have an issue with B4Xserializator: i have an array of string and i cannot use B4Xserializator on this array

B4X:
       Dim init(14) As String   
       for i = 1 to 14
       init(i) = "xxxx"
       next

'and  at last i want to use  B4Xserializator
        Log("htmldata:",sr.convertarraytobytes(init))           
        Main.SaveNetworkDetails(sr.ConvertArrayToBytes(init))
 
Upvote 0

candide

Active Member
Licensed User
sorry, objects concept is not clear for me.

by exemple, this case bolow is similar at case above, but less optimized and it is working well:

B4X:
     If bc.IndexOf(Buffer, "save?_nwk=") <> -1 Then

    Dim init(14) As Object
     Dim pt1,pt2 As Int

    pt1 = bc.IndexOf(Buffer, "save?_nwk=")+10            
    pt2 = bc.IndexOf(Buffer, "&_key=")
    init(0) = bc.substring2(Buffer,pt1,pt2)
    pt1 = pt2+6        
    pt2 = bc.IndexOf(Buffer, "&_mth=")
    init(1) =bc.substring2(Buffer,pt1,pt2)
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_mtp=")
    init(2) =bc.substring2(Buffer,pt1,pt2)
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_mtc=")
    init(3) =bc.substring2(Buffer,pt1,pt2)
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_mtu=")
    init(4) =bc.substring2(Buffer,pt1,pt2)
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_mtk=")
    init(5) =bc.substring2(Buffer,pt1,pt2)
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_ms1=")
    init(6) =bc.substring2(Buffer,pt1,pt2)
    init(6) = ReplaceString(init(6),"%2F","/")
    init(6) = ReplaceString(init(6),"%25","%")
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_ms2=")
    init(7) =bc.substring2(Buffer,pt1,pt2)
    init(7) = ReplaceString(init(7),"%2F","/")
    init(7) = ReplaceString(init(7),"%25","%")
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_ms3=")
    init(8) =bc.substring2(Buffer,pt1,pt2)
    init(8) = ReplaceString(init(8),"%2F","/")
    init(8) = ReplaceString(init(8),"%25","%")
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_mt1=")
    init(9) =bc.substring2(Buffer,pt1,pt2)
    init(9) = ReplaceString(init(9),"%2F","/")
    init(9) = ReplaceString(init(9),"%25","%")
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_mt2=")
    init(10) =bc.substring2(Buffer,pt1,pt2)
    init(10) = ReplaceString(init(10),"%2F","/")
    init(10) = ReplaceString(init(10),"%25","%")
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_ss1=")
    init(11) =bc.substring2(Buffer,pt1,pt2)
    init(11) = ReplaceString(init(11),"%2F","/")
    init(11) = ReplaceString(init(11),"%25","%")
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_ss2=")
    init(12) =bc.substring2(Buffer,pt1,pt2)
    pt1 = pt2+6
    pt2 = bc.IndexOf(Buffer, "&_save=")
    init(13) =bc.substring2(Buffer,pt1,pt2)

    For Each o As Object In init
        Log("html=",o)
    Next
        Log("htmldata:",sr.ConvertArrayToBytes(init))
        Main.SaveNetworkDetails(sr.ConvertArrayToBytes(init))

in this last example, i can create an array init(14) as objet, i can add string in each item and at last i can use sr.ConvertArrayToBytes but i don't understand why this one is working and why the first case is not working...

i will try to learn more about object.

thanks for your help
 
Upvote 0

candide

Active Member
Licensed User
i found original problem of my parse : i don't have same behavior when i use init(0) and init(nb) with nb = 0
init(0) = "azerty" and init(1)="qsddfg" ..... is working and at last sr.convertarraytobytes(init) is working


init(nb) = "azery" with nb = 0 and after init(nb) = "qsdfg" with nb = 1.....is not working and at last all items 0<13 will have same value 13
=> at last sr.convertarraytobytes(init) is not working

possible it due to mistake in my code but i don't understand where is my mistake, a small code to test it is attached.

thanks fr your help
 

Attachments

  • test.zip
    1.3 KB · Views: 232
Upvote 0

candide

Active Member
Licensed User
ok, it is possible to have better syntax but this case is OK

case not working in my case is when i want to use a variable from 0 to 13 to affect a string at item like init(nb)
in my case init() is object and i suppose it is linked to behavior seen

just i would like to understand what compiler is doing and how to have correct behavior.
 
Upvote 0

candide

Active Member
Licensed User
the simplest way to test what is my issue:
B4X:
        Dim init(14) As Object
        Dim nb As Int = 0
        Dim nkay() As String = Array As String("aaaa","bbbb","cccc","dddd","eeee","ffff","gggg","hhhh","iiii","jjjj","kkkk","llll","mmmm","nnnn")
        For Each n As String In nkay
            Log("n value=",n)
            init(nb) = n
            nb = nb +1
        Next
    Log(" ")
    Log("log or result in each item :")    
    nb = 0
    For Each o As Object In init
    Log("init(",nb,")=",o)
    nb = nb + 1
    Next
    Log(" ")
 
        Log("B4Rserializater result")

log is this one :

=> value a reading: it is the good one
n value=aaaa
n value=bbbb
n value=cccc
n value=dddd
n value=eeee
n value=ffff
n value=gggg
n value=hhhh
n value=iiii
n value=jjjj
n value=kkkk
n value=llll
n value=mmmm
n value=nnnn

interrogation of each item : all items have last value loaded in init(nb) with nb=13
init(0)=nnnn
init(1)=nnnn
init(2)=nnnn
init(3)=nnnn
init(4)=nnnn
init(5)=nnnn
init(6)=nnnn
init(7)=nnnn
init(8)=nnnn
init(9)=nnnn
init(10)=nnnn
init(11)=nnnn
init(12)=nnnn
init(13)=nnnn

B4Rserializater result
htmldata1=~nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
all items (0 to 13) have same value, the last loaded.
 
Upvote 0

candide

Active Member
Licensed User
and OK case :
B4X:
    Dim init(14) As Object
    Dim nb As Int = 0
    Dim nkay() As String = Array As String("aaaa","bbbb","cccc","dddd","eeee","ffff","gggg","hhhh","iiii","jjjj","kkkk","llll","mmmm","nnnn")
    init(0) = nkay(0)
    init(1) = nkay(1)
    init(2) = nkay(2)
    init(3) = nkay(3)
    init(4) = nkay(4)
    init(5) = nkay(5)
    init(6) = nkay(6)
    init(7) = nkay(7)
    init(8) = nkay(8)
    init(9) = nkay(9)
    init(10) = nkay(10)
    init(11) = nkay(11)
    init(12) = nkay(12)
    init(13) = nkay(13)
    Log(" ")
    Log("log or result in each item :")
    nb = 0
    For Each o As Object In init
        Log("init(",nb,")=",o)
        nb = nb + 1
    Next
    Log(" ")
    
    Log("B4Rserializater result")
    Log("htmldata1=",sr.convertarraytobytes(init))

log is :
log or result in each item :
init(0)=aaaa
init(1)=bbbb
init(2)=cccc
init(3)=dddd
init(4)=eeee
init(5)=ffff
init(6)=gggg
init(7)=hhhh
init(8)=iiii
init(9)=jjjj
init(10)=kkkk
init(11)=llll
init(12)=mmmm
init(13)=nnnn

B4Rserializater result
htmldata1=~aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnn
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is indeed a tricky case and the solution is:
B4X:
Dim bc As ByteConverter
Dim nkay() As String = Array As String("aaaa","bbbb","cccc","dddd","eeee","ffff","gggg","hhhh","iiii","jjjj","kkkk","llll","mmmm","nnnn")
For Each n As String In nkay
    Log("n value=",n)
    bc.ObjectSet(n, init(nb))
    nb = nb +1
Next

The problem in this code is in line 3:
B4X:
For Each n As String In nkay
            Log("n value=",n)
            init(nb) = n
            nb = nb +1
        Next
The string must be wrapped with an Object. The compiler does it for you however all the string elements in the nkay array will be wrapped with the same Object.
The solution is to use the special bc.ObjectSet method that copies the wrapped pointer from the temporary object to the object in the init array.
 
Upvote 0

candide

Active Member
Licensed User
Thanks Erel,
you are proving every days your level of expert and i was not able to find the solution alone.

More and more i think B4R is a wonderful tool, and with this forum it is easy and fast to do something, to test it and to make it working...

Thanks for your tool, thanks for your support, Thanks
 
Upvote 0
Top