Android Question Waiting until download ist ready?

schimanski

Well-Known Member
Licensed User
Longtime User
I'm going to download several .txt-files with the FTP-Lib, but it seems, that there are to many threads to make the job done. How is it possible to prevent the app from making the download in the background? I want to wait on each download until the app resumes.

Thanks for help...
 

DonManfred

Expert
Licensed User
Longtime User
Seems the Thread b4aErelPostingMachine is running twice :D
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I need a little bit help with my text-files:

I have a lot of text-files on my ftp-server, but I'm not able to download them completly. My firts question: Is it possible to get the text of the .txt-files directly or do I have to download each file and read the text with files.readstring?

My second question: When I have to download each file, when do I know, if the last file is downloaded? I tried it with the FTP-Lib, but it seems, that there are to many download-threads, when I start the downloads with a for-next-loop and I'm not able to check, if the last file is completed....After downloading, I want to write the text in a listview....

Did anybody wrote such a routine? I'm confused:(
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i have writte a small example for you.
Please read the comments. I hope it helps

B4X:
#Region  Project Attributes
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName:
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim FTP As FTP
   Dim ftpjobs As List
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("1")
  ftpjobs.Initialize
   If FirstTime Then
  FTP.Initialize("FTP", "domain.tld", 21, "username", "password")
  End If
   ' I know there are a lot of images in a folder... Let us take some of them and fill a "Job-List"
   ' You can fill this list with your textfiles (their names)
   ftpjobs.Add("9.jpg")
   ftpjobs.Add("10.jpg")
   ftpjobs.Add("11.jpg")
   ftpjobs.Add("12.jpg")
   ftpjobs.Add("13.jpg")
   ftpjobs.Add("14.jpg")
   ftpjobs.Add("15.jpg")
   ftpjobs.Add("16.jpg")
   ftpjobs.Add("17.jpg")
   ftpjobs.Add("18.jpg")
   ftpjobs.Add("18.jpg")
   ftpjobs.Add("20.jpg")
   ftpjobs.Add("21.jpg")
   ftpjobs.Add("22.jpg")
   ftpjobs.Add("23.jpg")
   ftpjobs.Add("25.jpg")
   ftpjobs.Add("26.jpg")
   ftpjobs.Add("28.jpg")
   ftpjobs.Add("29.jpg")
   ftpjobs.Add("30.jpg")
   ftpjobs.Add("31.jpg")
   ftpjobs.Add("32.jpg")
   ftpjobs.Add("33.jpg")
   ftpjobs.Add("36.jpg")
   ftpjobs.Add("35.jpg")
   
   ' to test the contents of the list
   For i = 0 To ftpjobs.Size -1
     Log("File: "&ftpjobs.Get(i))
   Next
   
   ' Now we start the first download
   ' after checking whether the joblist has entries or not
   If ftpjobs.Size > 0 Then
     ' Now we get the first item in this list and start download with ftp
     Label1.Text = "Downloading "&ftpjobs.Get(0)&"..."
     FTP.DownloadFile("/vdfb.de/gfx/rankimages/"&ftpjobs.Get(0), False, File.DirRootExternal, ftpjobs.Get(0))
     ' the further logic goe on in sub FTP_DownloadCompleted
   End If
End Sub

Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
  Dim s As String
  s = "Downloaded " & Round(TotalDownloaded / 1000) & "KB"
  If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
  Log(s)
End Sub
Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
  Log(ServerPath & ", Success=" & Success)
  If Success = False Then
       Log(LastException.Message)
     Else
       ' The file is successfully downloaded.
       '
       ' Delete the topmost job in ftpjobs
       If ftpjobs.Size > 0 Then
         ftpjobs.RemoveAt(0)
       End If
       
       ' Now we start the next download if there are more files in the joblist
       ' after checking whether the joblist has entries or not
       If ftpjobs.Size > 0 Then
         ' Now we get the first item (it was the next item before we deleted the first one ;-)))
         ' In this List AND start download with FTP
         Label1.Text = "Downloading "&ftpjobs.Get(0)&"..."
         FTP.DownloadFile("/vdfb.de/gfx/rankimages/"&ftpjobs.Get(0), False, File.DirRootExternal, ftpjobs.Get(0))
       Else
         Label1.Text = "All files have been downloaded"
         Log("All files have been downloaded")
       End If
       
     End If
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
** Activity (main) Create, isFirst = true **


File: 9.jpg
File: 10.jpg
File: 11.jpg
File: 12.jpg
File: 13.jpg
File: 14.jpg
File: 15.jpg
File: 16.jpg
File: 17.jpg
File: 18.jpg
File: 18.jpg
File: 20.jpg
File: 21.jpg
File: 22.jpg
File: 23.jpg
File: 25.jpg
File: 26.jpg
File: 28.jpg
File: 29.jpg
File: 30.jpg
File: 31.jpg
File: 32.jpg
File: 33.jpg
File: 36.jpg
File: 35.jpg
/vdfb.de/gfx/rankimages/9.jpg, Success=true
/vdfb.de/gfx/rankimages/10.jpg, Success=true
/vdfb.de/gfx/rankimages/11.jpg, Success=true
/vdfb.de/gfx/rankimages/12.jpg, Success=true
/vdfb.de/gfx/rankimages/13.jpg, Success=true
/vdfb.de/gfx/rankimages/14.jpg, Success=true
/vdfb.de/gfx/rankimages/15.jpg, Success=true
/vdfb.de/gfx/rankimages/16.jpg, Success=true
/vdfb.de/gfx/rankimages/17.jpg, Success=true
/vdfb.de/gfx/rankimages/18.jpg, Success=true
/vdfb.de/gfx/rankimages/18.jpg, Success=true
/vdfb.de/gfx/rankimages/20.jpg, Success=true
/vdfb.de/gfx/rankimages/21.jpg, Success=true
/vdfb.de/gfx/rankimages/22.jpg, Success=true
/vdfb.de/gfx/rankimages/23.jpg, Success=true
/vdfb.de/gfx/rankimages/25.jpg, Success=true
/vdfb.de/gfx/rankimages/26.jpg, Success=true
/vdfb.de/gfx/rankimages/28.jpg, Success=true
/vdfb.de/gfx/rankimages/29.jpg, Success=true
/vdfb.de/gfx/rankimages/30.jpg, Success=true
/vdfb.de/gfx/rankimages/31.jpg, Success=true
/vdfb.de/gfx/rankimages/32.jpg, Success=true
/vdfb.de/gfx/rankimages/33.jpg, Success=true
/vdfb.de/gfx/rankimages/36.jpg, Success=true
/vdfb.de/gfx/rankimages/35.jpg, Success=true
All files have been downloaded
** Activity (main) Pause, UserClosed = true **

PS: du hättest den Thread im Deutschen Forum anlegen sollen... *grins*
 
Last edited:
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Noch besser, ich wohn' nämlich in Breberen, also Gemeinde Gangelt...Am Ende kenn' ich deinen Freund noch....Schöne Grüsse auf jeden Fall..
 
Upvote 0
Top