Android Question split issue

Addo

Well-Known Member
Licensed User
Longtime User
i am trying to split the following data


B4X:
2CLIENTS~������������|
URL1~URL2~URL3~URL4~URL5~

using this way

B4X:
Public Sub NewData (data() As Byte)
    
Dim msg As String




msg = BytesToString(data, 0, data.Length, "UTF-8")
msg = msg.Trim
msg = msg.Replace(CRLF, "")
msg = msg.Replace(Chr(10), "")
msg = msg.Replace(Chr(13), "")





Dim param As String = msg
Dim paramnum() As String = Regex.Split("\~", param)

Log(paramnum(2))

End Sub

Paramnum(2) raise an exception java.lang.ArrayIndexOutOfBoundsException: length=1; index=2

i have been told in other thread from @DomManFred to remove BOM Header using bytesbuilder which i couldn't figure out how to use it correctly in this manner .

then from server side i already set WriteBOM To false but i am still getting the exception if i try to get Paramnum that its index (1) or (2) or (3)

The weird thing is when i loop through it

B4X:
For i = 0 To paramnum.Length-1
        Log(paramnum(i))
    Next

i see each one of the splited string without any problems i am trying from yesterday to see a workaround but i couldn't
 

DonManfred

Expert
Licensed User
Longtime User
as already written. You should not convert to a string if the data is not intended to be a string.

This may help you to understand what you are sending... In fact you are sending 0-bytes... 0x00

B4X:
Public Sub NewData (data() As Byte)
    Log($"NewData(${Bconv.HexFromBytes(data)})"$)
    Dim hex As String = Bconv.HexFromBytes(data)
    Dim hexparts() As String = Regex.Split("0D0A", hex)
    For i = 0 To hexparts.Length-1
        Dim plain As String = Bconv.StringFromBytes(Bconv.HexToBytes(hexparts(i)),"UTF8")
        Log($"${i}:${hexparts(i)} ->  ${plain}"$)
    Next
    
    'Dim msg As String
    'msg = BytesToString(data, 0, data.Length, "UTF-8")
    'Dim param As String = msg
    'Dim paramnum() As String = Regex.Split("\~", param)
    'For i = 0 To paramnum.Length-1
    'Log(paramnum(i)&i)
    'Next

End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
This code works:
B4X:
Public Sub NewData (data() As Byte)
    
    Dim msg As String

    msg = BytesToString(data, 0, data.Length, "UTF-8")
    msg = msg.Replace(Chr(0), "")
    msg = msg.Replace(Chr(1), "")
    
    Dim paramnum() As String = Regex.Split("\~" & Chr(13) & Chr(10), msg)
    For i = 0 To paramnum.Length-1
        Log(paramnum(i) & i)
    Next

End Sub

These are the bytes you get:
upload_2018-3-9_9-4-57.png
 
Upvote 0

Addo

Well-Known Member
Licensed User
Longtime User
I have tested it is not working try to test it paramnum(2) in the log exceptions will accure
You showing the looped results which paramnum(2) shouldn't show that pack of results if it was successfully splited either way outside the loop you will have exception raised
 
Upvote 0

Addo

Well-Known Member
Licensed User
Longtime User
If it was successfully splitted paramnum(2) should show iraq2 only
And what is piraq1 ? P does not represented any where in the data sent

Either ways log(paramnum(2)) will raise exception
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
can you save it as binary or something? then you can see what hidden stuff is inthere.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I have a lot of problems to get valid data returned so it's hard to test but this seems to work for me.

B4X:
Public Sub NewData (data() As Byte)
Dim msg As String

For x=0 To data.Length-1
    If data(x)=0 Then data(x)=0x21
    If data(x)=1 Then
        data(x)=0x21
        data(x+1)=0x21
    End If
Next
  
msg = BytesToString(data, 0, data.Length, "UTF-8")

Dim param As String = msg.Replace("!","")
Dim paramnum() As String = Regex.Split("~\r\n", param)

For x=1 To 5
    Log(paramnum(x))
Next
Log("---")
Log(paramnum(2))
End Sub

output:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Data sent
iraq
iraq
moroco
egypt
usa
---
iraq
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I now see that it similar to what @klaus did but in a different way to fix that first item as it was not clear that is should be Piraq, iraq or skipped.

the error that you might get is probably that data fetch issue I mentioned above when it just returns a "2GETCATS" string then the split will return nothing.

maybe it's easier to fix at the server side and remove the unneeded data?
 
Last edited:
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
Here is my attempt:

B4X:
Public Sub NewData (data() As Byte)
   
Dim msg As String
Dim APSU As ApacheSU
msg = BytesToString(data, 0, data.Length, "UTF-8")
msg =    Regex.Replace("[^a-zA-Z\d\s~:]",msg,"")
msg = APSU.Replace(msg,"2GETCATS","")
Dim param As String = APSU.DeleteWhitespace(msg)
Dim paramnum() As String = APSU.SplitWithSeparator(param,"~")
For i = 0 To paramnum.Length-1
Log(paramnum(i)&i)
Next
End Sub

Gives:

B4X:
iiraq0
moroco1
egypt2
usa3
yemen4
iraq5
jordan6
ksa7
ksa8
turkey9
qatar10
spain11
iraq12
lebanon13
moroco14
iraq15
syria16
iraq17
islamic18
iraq19
syria20
tunsia21
iraq22
sweeden23
iraq24
iraq25
iraq26
jordan27
iraq28
iraq29
iraq30
iraq31
egypt32
iraq33
palestine34
kwait35
uae36
algeria37
ksa38
jordan39
denmark40
bahrian41
france42
germany43
libya44

AppacheSU library from here
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
my guess is that your split in post is just split it into 2 items. and when you print item 1(0) it lists everything as a list due to the linefeeds.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
You are failing to understand what your code is actually doing. AStreams_NewData is being called twice That is you problem. Change your AStreams_NewData to:

B4X:
Sub AStream_NewData (Buffer() As Byte)
    Log("New Data Call: " & cntr)
    Log(BytesToString(Buffer,0,Buffer.Length,"UTF-8"))
    cntr =cntr+1
End Sub
Add:
[code]
Dim cntr As Int  = 1

Run your program and your log will look like this:

B4X:
Data sent
New Data Call: 1
2GETCATS~
New Data Call: 2
 
Upvote 0

Addo

Well-Known Member
Licensed User
Longtime User
No I know that I am sending string and stream yes I am aware that I am doing writeln then write at the same time which I think asyncstream can handle without problems which is not the fact
 
Upvote 0
Top