Android Question File Location / Httpjob

victormedranop

Well-Known Member
Licensed User
Longtime User
Hi, I need some assistance. with downloading files from web site.
with httpjob.

I have this code for downloading files
B4X:
dim playlistx as list
playlistx.Initialize
Dim url_location As String = "http://10.0.0.32/uploads/"
playlistx.Add( "free_1.jpg")
playlistx.Add( "free_2.jpg")
playlistx.Add( "free_3.jpg")
playlistx.Add( "free_4.jpg")
For i = 0 To playlistx.Size - 1
dd.Initialize(playlistx.Get(i), Me)
file_name = playlistx.Get(i)
Try
dd.Download(url_location & file_name)
Log("Download Filename : " & url_location & file_name)
Catch
Log(LastException)
End Try
Next

and log downloaded files but where are the files ??

B4X:
Sub JobDone(Job As HttpJob)
If Job.Success Then
Log("Succes Downloading file")
Dim Out As OutputStream
Out = File.OpenOutput(File.DirRootExternal ,file_name,False)
Log("Filename : " & file_name)
File.Copy2(Job.GetInputStream, Out)
Out.Close
Job.Release
bm.Initialize(File.DirRootExternal ,file_name)
blist.Add(bm)
Log("belist size : " & blist.Size )
Else
Log("Error downloading image: " & Job.JobName & CRLF & Job.ErrorMessage)
End If
End Sub

I have 2 days working with this mode, this is good aproche to do this?

Victor
 

victormedranop

Well-Known Member
Licensed User
Longtime User
I re-write the loop for download this files but
only download now the last file.
B4X:
For i = 0 To playlistx.Size - 1
Dim dd As HttpJob
dd.Initialize(playlistx.Get(i), Me)
file_name = playlistx.Get(i)
Try
dd.Download(url_location & playlistx.Get(i))
Log("Download Filename : " & url_location & playlistx.Get(i))
Catch
Log(LastException)
End Try
Next
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
For i = 0 To playlistx.Size - 1
Dim dd As HttpJob
dd.Initialize("Download", Me)
dim fname as string = playlistx.Get(i)
dd.tag = fname
dd.Download(url_location & playlistx.Get(i))
Log("Download Filename : " & url_location & playlistx.Get(i))
Next

B4X:
Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
        Log("JobName: "&Job.JobName)
        If Job.JobName = "Download" Then
            Dim fname As String = Job.Tag
      Dim OutStream As OutputStream
      Log("DownloadReady: "&Job.Tag)
      OutStream = File.OpenOutput(File.DirRootExternal, fname, False)
      File.Copy2(Job.GetInputStream,OutStream) ' save the file
      OutStream.Close
      Log(fname&" is written to "&File.DirRootExternal)
        End If
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
    Job.Release
End Sub
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
SOLVED
now I downloaded all files.

I change my code to :

B4X:
If Job.Success Then
Dim Out As OutputStream
Out = File.OpenOutput(File.DirRootExternal ,Job.JobName,True)
File.Copy2(Job.GetInputStream, Out)
Out.Close
Log("Succes Downloading file : " & Job.JobName)
Else
Log("Error downloading image: " & Job.JobName & CRLF & Job.ErrorMessage)
End If
Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
Job.Release
bm.Initialize(File.DirRootExternal ,Job.JobName)
blist.Add(bm)
Log("belist size : " & blist.Size)
 
Upvote 0
Top