Android Question Too slow getting data

Mattiaf

Active Member
Licensed User
Hi, I'm getting text from a website at the press of a button, but it seems way too slow and not async.

The data is being downloaded from a website that cointains plain text only, it is filtered in order to display only what matters to me and I replace the weed days from english to italian.
The output is never bigger than 20 lines
The code is fully tested and It does its work.
But it is very slow, to complete the output it takes not less than 20 s.
Is there a way to make it faster? Thanks

The code is


B4X:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Edittext1 As EditText
  
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Private Sub Button1_Click
    ScaricaTestoEFiltra
End Sub
Sub ScaricaTestoEFiltra()
    Try
        Dim url As String = "mywebsite.com"
        Dim j As HttpJob
        j.Initialize("", Me)
        j.Download(url)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log("Download success")
            Dim testoFiltrato As StringBuilder
            testoFiltrato.Initialize
            Dim lines() As String = Regex.Split("\n", j.GetString)
            For Each linea As String In lines
                Dim matcher1 As Matcher = Regex.Matcher("(.*hd7\.php.*|.*hd8\.php.*)", linea)
                If matcher1.Find Then
                    Log("Match found: " & linea)
                    testoFiltrato.Append(linea).Append(CRLF).Append(CRLF)
                Else
                    Dim giorniSettimana() As String = Array As String("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY")
                    For Each giorno As String In giorniSettimana
                        If linea.Contains(giorno) Then
                            Log("Day found: " & giorno)
                            linea = linea.Replace(giorno, TraduciGiorno(giorno))
                            testoFiltrato.Append(linea).Append(CRLF)
                            Exit
                        End If
                    Next
          
                End If
            Next
            Edittext1.Text = testoFiltrato.ToString
        Else
            Log("Download failed")
        End If
      
        j.Release
    Catch
        Log("Error: " & LastException.Message)
    End Try
End Sub

Sub TraduciGiorno(giorno As String) As String
    Select giorno
        Case "MONDAY"
            Return "LUNEDI"
        Case "TUESDAY"
            Return "MARTEDI"
        Case "WEDNESDAY"
            Return "MERCOLEDI"
        Case "THURSDAY"
            Return "GIOVEDI"
        Case "FRIDAY"
            Return "VENERDI"
        Case "SATURDAY"
            Return "SABATO"
        Case "SUNDAY"
            Return "DOMENICA"
        Case Else
            Return giorno
    End Select
End Sub
 

JohnC

Expert
Licensed User
Longtime User
I would insert lines like these in various parts of your code to see where the bottleneck is:

B4X:
Log("This is before the download method: " & DateTime.Now)
 
Log("This is after the download method: " & DateTime.Now)

Log("This is the start of a new for/each loop: " & DateTime.Now)
 
 '....etc
 
Last edited:
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
Hi Mattia, I'm Sergio from Vicenza (Italy)

As @JohnC wrote, the main question is understand if it is too slow for the download problem or for the "for each line" loop

For example, how many times do you execute this line ?
B4X:
Dim giorniSettimana() As String = Array As String("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY")
You need to move it at the start of the Sub


You can also optimize TraduciGiorno sub
You need to join TraduciGiorno and giorniSettimana array in a Map object, so you don't need to use a Select statment
 
Last edited:
Upvote 0

Mattiaf

Active Member
Licensed User
Thanks everyone, Im going to process your answers on my code. I now get the logic you guys r explaining.
 
Upvote 0
Top