iOS Question Error parsing text file downloaded from web true httpjob - [Solved]

sdesan

Member
Licensed User
Longtime User
Hi,
i download some information from web server in this format

2008 10example data - (example) - data;200

2014 example data - (example) - data 2;2000

First 4 char are a code
From 6 char to ; is a description
After ; is a value

For extract data from downloaded information i use this code:

B4X:
Private Sub promozioni_Click
    mostraofferte1.Initialize("mostraofferte")
    mostraofferte1.RootPanel.LoadLayout("mostraofferte")
    mostraofferte1.Title="Ciao " & nomeutente & " hai " & totpunti & " punti"
    Dim linea As String
    Dim dove As Int
    Dim xui As XUI
    Dim codice As String
    Dim punti As String
    Dim i As Int
    LstPromozioni.Clear
    NavControl.ShowPage(mostraofferte1)
    Try
    Dim j As HttpJob
    j.Initialize("j", Me)
        j.Download("https://myserver.web/showdata.asp")
        Wait For (j) JobDone(j As HttpJob)
        'Sleep(500)
        If j.Success Then
            If j.GetString<>"KO" Then
                Dim out As OutputStream = File.OpenOutput(File.DirDocuments, "promozioni.txt", False)
                File.Copy2(j.GetInputStream, out)
                out.close
                ''File.WriteString(File.DirDocuments,"promozioni.txt",j.GetString)
                mylist=File.ReadList(File.DirDocuments,"promozioni.txt")
                i=0
                If mylist.size>1 Then
                    For i= 0 To mylist.Size-1
                        linea=mylist.Get(i)
                        dove=linea.IndexOf(";")
                        codice=linea.SubString2(0,4)
                        promozione=linea.SubString2(5,dove)
                        punti=linea.SubString(dove+1)
                        If i Mod 2 =0 Then
                            LstPromozioni.DefaultTextBackgroundColor=Colors.RGB(0,128,0)
                            LstPromozioni.DefaultTextColor=Colors.White
                        Else
                            LstPromozioni.DefaultTextBackgroundColor=Colors.white
                            LstPromozioni.DefaultTextColor=Colors.Black
                        End If
                        Log(promozione&CRLF&punti)
                        LstPromozioni.AddTextItem(promozione&CRLF&punti,codice)
                    Next
                End If
            End If
        End If
        j.Release
    Catch
        Log(LastException.Message)
    End Try
End Sub

When i run this code alla works fine but variable promozione obtained from line 513 (Main)

B4X:
 promozione=linea.SubString2(5,dove)

shows this error

Class (b4i_httpjob) instance released.
Class (b4i_customlistview) instance released.
Class (b4i_httpjob) instance released.
10
Error occurred on line: 513 (Main)
Cannot parse: example data - (example) - data 2
Stack Trace: (
CoreFoundation 14B73572-A1BC-3C77-A58A-A64A28663D46 + 38040
libobjc.A.dylib objc_exception_throw + 56
CoreFoundation 14B73572-A1BC-3C77-A58A-A64A28663D46 + 964464
ReversApp -[B4I ObjectToNumber:] + 336
ReversApp -[B4IRDebugUtils numberCast::] + 260
CoreFoundation 14B73572-A1BC-3C77-A58A-A64A28663D46 + 450496
CoreFoundation 14B73572-A1BC-3C77-A58A-A64A28663D46 + 127560
ReversApp +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1296
ReversApp -[B4IShell runMethod:] + 420
ReversApp -[B4IShell raiseEventImpl:method:args::] + 1472
ReversApp -[B4IShellBI raiseEvent:event:params:] + 1360
ReversApp -[B4IDebugResumableSub resume::] + 352
ReversApp -[B4I checkAndRunWaitForEvent:event:params:] + 440
ReversApp -[B4IShellBI raiseEvent:event:params:] + 1216
ReversApp -[B4ICommon CallSub4::::] + 240
ReversApp -[B4ICommon CallSub2::::] + 272
ReversApp -[b4i_httpjob _complete::] + 620
ReversApp -[b4i_httputils2service _completejob::::] + 1276
ReversApp -[b4i_httputils2service _hc_responsesuccess::] + 452
CoreFoundation 14B73572-A1BC-3C77-A58A-A64A28663D46 + 450496
CoreFoundation 14B73572-A1BC-3C77-A58A-A64A28663D46 + 127560
ReversApp +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1296
ReversApp -[B4IShell runMethod:] + 420
ReversApp -[B4IShell raiseEventImpl:method:args::] + 1968
ReversApp -[B4IShellBI raiseEvent:event:params:] + 1360
ReversApp __61-[B4IHttp URLSession:downloadTask:didFinishDownloadingToURL:]_block_invoke + 308
libdispatch.dylib A893D1C3-FCC0-3FBA-8AED-CBDC8C4C37CE + 411520
libdispatch.dylib A893D1C3-FCC0-3FBA-8AED-CBDC8C4C37CE + 289572
libdispatch.dylib A893D1C3-FCC0-3FBA-8AED-CBDC8C4C37CE + 411520
libdispatch.dylib A893D1C3-FCC0-3FBA-8AED-CBDC8C4C37CE + 286224
libdispatch.dylib A893D1C3-FCC0-3FBA-8AED-CBDC8C4C37CE + 285320
CoreFoundation 14B73572-A1BC-3C77-A58A-A64A28663D46 + 596524
CoreFoundation 14B73572-A1BC-3C77-A58A-A64A28663D46 + 480968
CoreFoundation CFRunLoopRunSpecific + 584
GraphicsServices GSEventRunModal + 160
UIKitCore DF57BFBD-C5E1-38E0-9009-CA7394D39A34 + 3608536
UIKitCore UIApplicationMain + 312
ReversApp main + 100
dyld DA5F527D-AD76-324B-AB9C-254FC806F191 + 82756
)
Cannot parse: example data - (example) - data 2

Code is able to show only, if present, first number in line (like in '2008 10example data - (example) - data;200' )
If line starts with string (like in '2014 example data - (example) - data 2;2000) the the previous error is shown
So i think that my error is in any variable definition or in some error in conversion in another type of variable
I've tried everything but can't figure out where is the error
Any help is welcome
Thank you
 

sdesan

Member
Licensed User
Longtime User
Thanks Emexes for the answer, unfortunately I don't understand what you want me to see / note

Same code in B4A works whitout any problem 🤷‍♂️
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
B4X:
Dim sample As String = _
$"2008 10example data - (example) - data;200

2014 example data - (example) - data 2;2000"$

Dim pattern As String = "([0-9]{4})([^;]*);([0-9]*)"    '4 digits, followed by description terminated by semicolon, followed by (numeric, possibly empty) value

Dim Matcher1 As Matcher
Matcher1 = Regex.Matcher(pattern, sample)
Do While Matcher1.Find
    If Matcher1.GroupCount >= 3 Then
        Log("Found:")
        Log(TAB & Matcher1.Group(1))
        Log(TAB & Matcher1.Group(2).Trim)
        Log(TAB & Matcher1.Group(3))
    End If
Loop

Log output:
Waiting for debugger to connect...
Program started.
Found:
    2008
    10example data - (example) - data
    200
Found:
    2014
    example data - (example) - data 2
    2000
Program terminated (StartMessageLoop was not called).
 
Upvote 0

sdesan

Member
Licensed User
Longtime User
It looks like you are trying to convert a string to number.
Probably here:
B4X:
linea=mylist.Get(i)
                        dove=linea.IndexOf(";")
                        codice=linea.SubString2(0,4)
                        promozione=linea.SubString2(5,dove)
                        punti=linea.SubString(dove+1)
thank you Erel,
based on your report I found that the variable 'promozione' had been set to Public as integer.
My mistake, thanks for the help
 
Upvote 0

sdesan

Member
Licensed User
Longtime User
B4X:
Dim sample As String = _
$"2008 10example data - (example) - data;200

2014 example data - (example) - data 2;2000"$

Dim pattern As String = "([0-9]{4})([^;]*);([0-9]*)"    '4 digits, followed by description terminated by semicolon, followed by (numeric, possibly empty) value

Dim Matcher1 As Matcher
Matcher1 = Regex.Matcher(pattern, sample)
Do While Matcher1.Find
    If Matcher1.GroupCount >= 3 Then
        Log("Found:")
        Log(TAB & Matcher1.Group(1))
        Log(TAB & Matcher1.Group(2).Trim)
        Log(TAB & Matcher1.Group(3))
    End If
Loop

Log output:
Waiting for debugger to connect...
Program started.
Found:
    2008
    10example data - (example) - data
    200
Found:
    2014
    example data - (example) - data 2
    2000
Program terminated (StartMessageLoop was not called).
Thank you very much Emexes. I will use your code in other apps too.;)
Thanks again for the replies and support
 
Upvote 0

sdesan

Member
Licensed User
Longtime User
BTW, I'm not sure that these variables should be global. Prefer local variables over global.
Thank you Erel but i this app i use 4 variables that must be visible in all sub and this is the reason why I called them global.
The app https://apps.apple.com/it/app/reversapp/id6444201207 is designed to manage the loyalty points of a plastic bottle recycling system and the values of the total points and other things I manage them through global variables because they are displayed in multiple screens and the most functions.
I believe that this is right but I am 'open' to any advice.
Thank you again 🙏
 
Upvote 0
Top