Android Question Problems getting file in time for activity

Penfound

Active Member
Licensed User
Longtime User
I am still struggling with getting a file either by httpUtils or FTP updated in time for the activity to be able to work with it.

If I use the FTP sub the file is downloaded if I am running in debug mode but the activity is too fast for it when running in release mode.

Alternatively, if I use the httpUtils method of getting the file it is not downloaded at all.

Can someone tell where I am going wrong please?

B4X:
Sub UpdateHTTPFiles
If CheckConnection Then
RemoveOldFiles
Dim dd As DownloadData
Dim tmpTarget As String = File.Combine(File.DirRootExternal,"/diary.xml")
dd.url = link1
dd.EventName = "dd"
dd.Target = tmpTarget
CallSubDelayed2(DownloadService, "StartDownload", dd)
If File.Exists(File.DirRootExternal, "/diary.xml") Then
File.Copy(File.DirRootExternal,"/diary.xml",File.DirDefaultExternal,"/diary.xml")
File.Delete(File.DirRootExternal,"/diary.xml")
End If
parser.Initialize
SQL1.Initialize(File.DirDefaultExternal, "/diary.db", True)
CreateTables
ProgressDialogHide
ToastMessageShow("Files Updated!",True)
End If
End Sub


Sub UpdateFiles
If CheckConnection Then
RemoveOldFiles
ToastMessageShow("Checking for latest files",True)
FTP.Initialize("FTP", "FTPSite", 21, "Username", "Password")
FTP.DownloadFile("public_html/BMB/diary.xml", False, File.DirRootExternal, "/diary.xml")
If File.Exists(File.DirRootExternal, "/diary.xml") Then
File.Copy(File.DirRootExternal,"/diary.xml",File.DirDefaultExternal,"/diary.xml")
File.Delete(File.DirRootExternal,"/diary.xml")
End If
parser.Initialize
SQL1.Initialize(File.DirDefaultExternal, "/diary.db", True)
CreateTables
ProgressDialogHide
ToastMessageShow("Files Updated!",True)
End If
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
FTP.DownloadFile("public_html/BMB/diary.xml", False, File.DirRootExternal, "/diary.xml")
' At THIS point the file is NOT downloaded... The Job is still running. You get an event fired 
' if the job has finished its work.
' You then can in this Sub do the next steps in your logic (copy the file to somewhere and so on)
If File.Exists(File.DirRootExternal, "/diary.xml") Then
 
Upvote 0

Penfound

Active Member
Licensed User
Longtime User
B4X:
FTP.DownloadFile("public_html/BMB/diary.xml", False, File.DirRootExternal, "/diary.xml")
' At THIS point the file is NOT downloaded... The Job is still running. You get an event fired
' if the job has finished its work.
' You then can in this Sub do the next steps in your logic (copy the file to somewhere and so on)
If File.Exists(File.DirRootExternal, "/diary.xml") Then

I think, because the FTP process runs in the background, the FileCopy executes before the FTP download has finished,. However, see next post :)
 
Upvote 0

Penfound

Active Member
Licensed User
Longtime User
Cracked it!

I kept reading Erel's bits on the FTP process then changed the following sub to make it all work.

B4X:
Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
Log(ServerPath & ", Success=" & Success)
If Success = TrueThen
Msgbox("Finished the file","Hi!")
Splash_Complete
Else
Msgbox(LastException.Message,"Error")
End If
End Sub

At the moment this works perfectly by delaying the activity a little till the file has downloaded. I realise though that as the file gets larger or connection slower, it will take longer than the 5 seconds allowed. Hopefully, by then I might have figured out another way - play a video or something :)
Of course that it will not work. You should handle the event. See the tutorial...

I know but I didn't know how.
 
Upvote 0
Top