Android Tutorial Download huge files with HttpUtils2

Discussion in 'Tutorials & Examples' started by Erel, Jun 13, 2013.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the code?
     
  2. ibra939

    ibra939 Active Member Licensed User

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

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

    Sub Process_Globals
    Private link1 As String = "http://moysar.com/download/molay.mp3"
    Private link2 As String = "http://moysar.com/download/molay.mp3"


    End Sub

    Sub Globals
    Dim ProgressBar1 As ProgressBar
    Dim Label1 As Label


    Dim btnCancel As Button
    Dim OutStream As OutputStream
    Private Button1 As Button
    Private WebView1 As WebView
    Private B1 As Button
    Private Button2 As Button
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    End Sub


    Sub dd_Progress(Progress As Long, Total As Long)
    ProgressBar1.Progress = Progress / Total * 100
    Label1.Text = NumberFormat(Progress / 1024, 0, 0) & "KB / " & _
    NumberFormat(Total / 1024, 0, 0) & "KB"
    End Sub

    Sub dd_Complete(Job As HttpJob)
    Log("Job completed: " & Job.Success)
    If Job.Success Then
    Dim OutStream As OutputStream
    Log("DownloadReady: "&Job.Tag)
    OutStream = File.OpenOutput(File.DirRootExternal, Job.Tag, False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
    File.Copy2(Job.GetInputStream,OutStream) ' save the file
    OutStream.Close
    End If
    Job.Release
    End Sub

    Sub btnCancel_Click
    'If Select
    ' Case:1
    CallSubDelayed2(DownloadService, "CancelDownload", link1)
    ' Case:2
    CallSubDelayed2(DownloadService, "CancelDownload", link2)

    'End If
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub Button1_Click
    Dim dd As DownloadData
    'dd.url =
    ' WebView1.LoadUrl("http://tvquranserver.com/download/TvQuran.com__Athkar/TvQuran.com_athkar_10.mp3")
    dd.url = link1
    dd.EventName = "dd"
    dd.Target = Me


    CallSubDelayed2(DownloadService, "StartDownload", dd)
    End Sub
    Sub Bn1_Click
    Activity.RemoveAllViews
    Activity.LoadLayout("Azker")
    End Sub
    Sub Button11_Click
    ' WebView1.LoadUrl("http://tvquranserver.com/download/TvQuran.com__Athkar/TvQuran.com_athkar_10.mp3")

    End Sub

    Sub Button2_Click
    ' WebView1.LoadUrl("http://tvquranserver.com/download/TvQuran.com__Athkar/TvQuran.com_athkar_01.mp3")
    End Sub
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    The problem is that Job.Tag doesn't hold the file name. It holds an object of type JobTag which is used internally.
     
    ibra939 likes this.
  4. ibra939

    ibra939 Active Member Licensed User

    you mean i add link1 to call the code Private link1 As String


    or i add "http://moysar.com/download/molay.mp3"


    Is't current following code i inserted ?


    Sub dd_Complete(Job As HttpJob)
    Log("Job completed: " & Job.Success)
    If Job.Success Then
    Dim OutStream As OutputStream
    Log("DownloadReady: "&link1)
    OutStream = File.OpenOutput(File.DirRootExternal, link1 ,False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job

    File.WriteString(File.DirDefaultExternal,link1, Job.GetString)

    Log(File.ReadString(File.DirDefaultExternal, link1))

    File.Copy2(Job.GetInputStream,OutStream ) ' save the file
    OutStream.Close
    Else
    Log("Error: " & Job.ErrorMessage)
    ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
    End Sub
     
    Last edited: Oct 27, 2014
  5. ibra939

    ibra939 Active Member Licensed User

    Erel new error thanks for you help......Iam trying to do it
     
  6. DonManfred

    DonManfred Expert Licensed User

    Without posting the error you say you get it is hard to imagine what you are doing.....

    LEARN TO WRITE GOOD HELP-REQUESTS!

    - Post the code you are using (not part of them. better the project exported as zip)
    - and the EXACT error you got (NOT A JPG, the error from log as text).
    - describe when exactly you get this error and what you have tried so far (the code you tried)

    With the infos you provided i just can use a wonderglas or even guess something which could be the problem! I don´t want to help with so much less informations provided!

    .
    .

    For the others who read this here; i got an personal mail from @ibra939 to help with this problem and he gave me just the link to page 7 of this thread. That´s all... So i read the last post and read a "i´ll try it"... lol
     
  7. ibra939

    ibra939 Active Member Licensed User


    I make the program so exported as zip file and when i found the errer i con't fix it because when i change job.tag give me same error i sent u because you have expert more then me also Erel he help me and i post the error code with the program .....o_O:eek::(
     
  8. DonManfred

    DonManfred Expert Licensed User

    Ok. then AGAIN cause i DONT WANT to search for any error you get (it´s your part to give us these information!):

    - Post the code you are using (not part of them. better the project exported as zip)
    - Post the EXACT error you got (NOT A JPG, the error from log as text).
    - describe when exactly you get this error and what you have tried so far (the code you tried)
     
  9. ibra939

    ibra939 Active Member Licensed User

    Thanks for Replay DonManfred the file with Error log ( http://www.mediafire.com/download/iop3vdia8iv8zl9/project.zip )

    jest i want the mp3 file download to memory SD or phone memory
     

    Attached Files:

    Last edited: Oct 28, 2014
  10. DonManfred

    DonManfred Expert Licensed User

    Code:
    Sub dd_Complete(Job As HttpJob)
        
    Log("Job completed: " & Job.Success )
      
    If Job.Success Then

            
    ' You are missing these lines....
            Dim JobTag As JobTag = job.Tag
             
    Dim dldta As DownloadData=JobTag.Data

        
            
    Dim OutStream As OutputStream
          
    Log("DownloadReady: "&dldta.url )
          
    ' -> DownloadReady: http://moysar.com/download/molay.mp3

          
    ' Please note the changes here too
          OutStream = File.OpenOutput(File.DirRootExternal,GetFilename(dldta.url),False' Getting filename with sub getfilename giving dldta.url to read the Original Filename we specify earlier in the creation of the Job

          
    File.Copy2(Job.GetInputStream,OutStream ) ' save the file
          Log(GetFilename(dldta.url)&" written to DirRootExternal")
          
    ' -> molay.mp3 written to DirRootExternal
            OutStream.Close
          
    ' The file should now be accessible in File.DirRootExternal
          ' Play the file now for ex.
         Else
            
    Log("Error:  " & Job.ErrorMessage)
            
    ToastMessageShow("Error:  " & Job.ErrorMessage, True)
        
    End If
        Job.Release
    End Sub
    Please note that you need the sub GetFilename from here too!
     
    Last edited: Oct 29, 2014
    ibra939 likes this.
  11. ibra939

    ibra939 Active Member Licensed User


    Test program ....... Thanks DonManfred o_O and checking GetFilename
     
  12. cambopad

    cambopad Active Member Licensed User

    How can I have more than two progress bars to track the progress of each downloading file?
     
  13. aidymp

    aidymp Active Member Licensed User

    With 2 event names and 2 progress subs!

    Code:
    Sub Button1_Click
    Dim download1 As DownloadData
    download1.url = 
    "http://file1.com"
    download1.EventName = 
    "download1"
    download1.Target = Me
    end sub

    Sub Button2_Click
    Dim download2 As DownloadData
    download2.url = 
    "http://file1.com"
    download2.EventName = 
    "download2"
    download2.Target = Me
    end sub

    Sub download1_Progress(Progress As Long, Total As Long)
    ProgressBar1.Progress = Progress / Total * 
    100
    Label1.Text = 
    NumberFormat(Progress / 102400) & "KB / " & _
    NumberFormat(Total / 102400) & "KB"
    End Sub

    Sub download2_Progress(Progress As Long, Total As Long)
    ProgressBar1.Progress = Progress / Total * 
    100
    Label1.Text = 
    NumberFormat(Progress / 102400) & "KB / " & _
    NumberFormat(Total / 102400) & "KB"
    End Sub
     
    cambopad likes this.
  14. cambopad

    cambopad Active Member Licensed User

    Thanks you for this suggestion, but How can I do if I want to download a lot of files without knowing about the number of downloads (I can say the number of progress subs) in advance?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Do you want to download all of these files at once?

    You can create a class instance for each download that will handle the event (set it with the Target parameter). However you are probably making it more complicated than it should be.

    You can decide to download up to 3 files at once and then start the next download each time the previous one completes. The overall download time will most probably be the same.
     
    cambopad likes this.
  16. cambopad

    cambopad Active Member Licensed User

    Thank @Erel Yes, I want to download all of these files at once with their own progress bar! How to do that?
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Create a class that holds a ProgressBar and use it to handle the events. Now you can create an instance of this class for each download.
     
    cambopad likes this.
  18. cambopad

    cambopad Active Member Licensed User

    For this method, do we still need to write many progress subs by hand? For example,
    sub download2_Progress, sub
    download3_Progress,...?
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    No. Only a single one.
     
  20. zeuspower

    zeuspower Member Licensed User

    Works OK with me,but with one problem,I don't get Total filesize

    Probably because my file is on my Google Drive and the link is HTTPS ...
    any suggestions ?
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice