Android Question httputils downloading file from link-Wait for being ignored

AusSteve

Member
Licensed User
Longtime User
Hi,

I'm trying to read a file from a google drive folder. The file is shared so anyone can view it and the link works.

I've been trying to follow the guides but I'm stuck. I have copied the code attached. program execution reaches the Wait For line, and then sub execution ceases and the code returns to the calling module.

called sub:
Sub DownloadAndSaveFile (Link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)

    If j.Success Then
        Log ("Success")
        Dim out As OutputStream = File.OpenOutput(xui.DefaultFolder, CSVFile, False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
    End If
    j.Release
End Sub

The outputstream is not triggered, the file isn't saved.

Despite that, testing for File.Exists returns true, but the file can't be found in the destination folder.

any clues????
 

drgottjr

Expert
Licensed User
Longtime User
1) works for me. i copied and pasted your sbu, assigned CSVFILE, called the sub, and it ran without a problem (as expected).
2) file listed without a problem.
3) i assume you know you can't "see" files in xui.defaultfolder if you're trying to find them from your attached pc. they can be listed, however. is this what you did?
4) i'm confused by your terms "sub execution" and "calling module". you should probably show some more code if file.exists was true, and the answer to question in #3 was yes.
 
Upvote 0

AusSteve

Member
Licensed User
Longtime User
the current code is :
B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
'    Private Activity As Activity

    Private B4XTable1 As B4XTable
    Private const CSVFile As String = "songlist.csv"
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("CMCK_Song_List")
    xui.SetDataFolder(File.DirAssets)
    B4XTable1.RowHeight = 30dip
    B4XTable1.NumberOfFrozenColumns = 1
    B4XTable1.AddColumn("Song", B4XTable1.COLUMN_TYPE_TEXT)
    B4XTable1.AddColumn("Singer", B4XTable1.COLUMN_TYPE_TEXT)
    DownloadAndSaveFile("https://drive.google.com/file/d/1q9nKZ6SqiegtLiq64pVJbk9fTuc5AMpS/view?usp=sharing")
    LoadData
    B4XTable1.MaximumRowsPerPage = 30
    B4XTable1.BuildLayoutsCache(B4XTable1.MaximumRowsPerPage)
    
End Sub


Private Sub LoadData
    
    Dim data As List
    data.Initialize
    If File.Exists(xui.DefaultFolder,CSVFile) Then
         Dim su As StringUtils
         data = su.LoadCSV(xui.DefaultFolder, CSVFile, ",")
    Else
           data.Initialize
    End If
    B4XTable1.SetData(data)
End Sub

Sub DownloadAndSaveFile (Link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log ("Success")
        Dim out As OutputStream = File.OpenOutput(xui.DefaultFolder, CSVFile, False)
        File.Copy2(j.GetInputStream, out)
        out.Close '<------ very important
    End If
    j.Release
End Sub

The layout has a table in a panel, plus a couple of labels.

I am trying to load the data to the table, I have these libraries added

1641704067462.png
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
sub execution ceases and the code returns to the calling module.
That is expected behaviour and is how Resumable Subs work.
 
Upvote 1
Top