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
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
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
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
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
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?
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
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
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