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


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
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).
 
Upvote 0

sdesan

Member
Licensed User
Longtime User
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
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…