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

    It is not related to https. The total size is fetched from the content length header.

    I guess that in this case the server doesn't return the correct value in this header.
     
  2. zeuspower

    zeuspower Member Licensed User

    ΟΚ,Thanks !
     
    Last edited: May 4, 2015
  3. Ricardo Bunschoten

    Ricardo Bunschoten Member Licensed User

    If i want to write the download file to a folder like the default folder Download in the internal or external memory ? is that possible instead of to the android data/app dir ?

    And if so whats the exact code then ?

    I have now this code and with the help of this topic it downloads the zipfile and saves it in the android data/application name folder

    But i want it to be in the default Download dir of the internal memory or on a sd card and then in a specified folder

    Second if i download like a apk file with this script is it also possible in B4a to execute that downloaded apk so it's been installed ?

    Code:
    #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
        
    Private link1 As String = "http://download.thinkbroadband.com/20MB.zip"
    End Sub

    Sub Globals
        
    Dim ProgressBar1 As ProgressBar
        
    Dim Label1 As Label
        
    Dim btnDownload As Button
        
    Dim btnCancel As Button
    End Sub

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

    Sub btnDownload_Click
        
    Dim dd As DownloadData
        dd.url = link1
        dd.EventName = 
    "dd"
        dd.Target = Me
        CallSubDelayed2(DownloadService, 
    "StartDownload", dd)
    End Sub

    Sub dd_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 dd_Complete(Job As HttpJob)
        
    Log("Job completed: " & Job.Success)

        
    Dim o As OutputStream
        o = 
    File.OpenOutput(File.DirDefaultExternal, "20mb.zip"False)
        
    File.Copy2(Job.GetInputStream, o)
        o.Close
        Job.Release
    End Sub

    Sub btnCancel_Click
        CallSubDelayed2(DownloadService, 
    "CancelDownload", link1)
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub
     
    Last edited: May 21, 2015
  4. Erel

    Erel Administrator Staff Member Licensed User

    It is possible and there are several threads that explain how to do it. Start a new thread if you don't find them.

    Your code will copy the file to DirDefaultExternal. What exactly do you want to change?
     
  5. Ricardo Bunschoten

    Ricardo Bunschoten Member Licensed User

    Thnx for the reply.

    The code copiess/saves the file to android/data/applicationmamefolder/files/

    I want the downloaded file to be saved somewhere else.

    Android has a default folder named Download

    I want the file to be saved in that Downloadfolder.

    Standard that folder is located in internal sdcard/Download
     
    Last edited: May 22, 2015
  6. Erel

    Erel Administrator Staff Member Licensed User

    The downloads folder is File.Combine(File.DirRootExternal, "Download").

    Copying the file to this folder is simple. If you wan to save it directly to this folder then you will need to understand how HttpUtils2 works and modify it (or use HttpClient directly).
     
  7. Ricardo Bunschoten

    Ricardo Bunschoten Member Licensed User

    Thank you will see how that will work out.

    Directly save is not needed copy is also good.
     
  8. Ricardo Bunschoten

    Ricardo Bunschoten Member Licensed User

    OK i managed to change the code so that the file is beeing downloaded to the internal folder Download
    Not like you said but this also works

    Code:
    #Region  Project Attributes
        
    #ApplicationLabel: Veolo 4K Kodi Skin installer
        
    #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
        
    Private link1 As String = "https://copy.com/6R9j8KkEkocJTU3k/WINDOWS+XP.pdf"
    End Sub

    Sub Globals
        
    Dim ProgressBar1 As ProgressBar
        
    Dim Label1 As Label
        
    Dim btnDownload As Button
        
    Dim btnCancel As Button
    End Sub

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

    Sub btnDownload_Click
        
    Dim dd As DownloadData
        dd.url = link1
        dd.EventName = 
    "dd"
        dd.Target = Me
        CallSubDelayed2(DownloadService, 
    "StartDownload", dd)
    End Sub

    Sub dd_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 dd_Complete(Job As HttpJob)
        
    Log("Job completed: " & Job.Success)

        
    Dim o As OutputStream
        o = 
    File.OpenOutput(File.dirrootExternal, "Download/Windowsxp.pdf"False)
        
    File.Copy2(Job.GetInputStream, o)
        o.Close
        Job.Release
    End Sub

    Sub btnCancel_Click
        CallSubDelayed2(DownloadService, 
    "CancelDownload", link1)
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub
    The part
    Code:
    Dim o As OutputStream
        o = 
    File.OpenOutput(File.dirrootExternal, "Download/Windowsxp.pdf"False)
        
    File.Copy2(Job.GetInputStream, o)
        o.Close
        Job.Release
    End Sub
    Saves it to the default download folder.
     
  9. fixit30

    fixit30 Active Member Licensed User

    You should really check that Job.Success = True before trying to save the file otherwise it could cause you issues if the job fails.
     
  10. Ricardo Bunschoten

    Ricardo Bunschoten Member Licensed User

    Yes indeed thnx
     
  11. Ricardo Bunschoten

    Ricardo Bunschoten Member Licensed User

    Question. I want to download files from a bunch of urls. These urls are specified in a txt file and this file located on a webserver

    Example http://www.acryan.com/Veolo4kskin/url.txt
    This file contains the filename and url

    Is this possible ?

    You can use the List1 = File.ReadList(File.DirAssets, "url.txt") option but then you have to put the txt file local and i want it online so i can change the urls without recompiling the app :)

    is there a example maybe available how to to this ?
     
  12. DonManfred

    DonManfred Expert Licensed User

  13. Ricardo Bunschoten

    Ricardo Bunschoten Member Licensed User

    I know i have to parse it but can not find in the documentation how to do that ;)


    I know use this
    Code:
    Sub btnDownload_ClickDim dd As DownloadData
     dd.url = link1
     dd.EventName = 
    "dd"
     dd.Target = Me
     CallSubDelayed2(DownloadService, 
    "StartDownload", dd)
    where link 1 is specified in Sub Process_GlobalsPrivate link1 As String = https://copy.com/f0m16GtdU2h7wIqz/test.zip
     
  14. ibra939

    ibra939 Active Member Licensed User



    This is working if i make URL for any thing ? any one test it
     
  15. Ricardo Bunschoten

    Ricardo Bunschoten Member Licensed User

    what to test then ?
     
  16. Mashiane

    Mashiane Expert Licensed User

    Hi I cant compile this app, its giving me an error when I try to run

    Code:
    src\b4a\example\httputils2service.java:144: error: cannot access ParseException
    anywheresoftware.b4a.keywords.Common.Log(_response.GetString(
    "UTF8"));
                                                                ^
      class 
    file for org.apache.http.ParseException not found
     
  17. DonManfred

    DonManfred Expert Licensed User

    the class is using the "old" httputils instead of the new provided with okHTTP?
    Or you are using android 6? In this case you need to update your httputils with the okHTTP version of them
     
    lemonisdead and Mashiane like this.
  18. Mashiane

    Mashiane Expert Licensed User

    Thanks, solves everything...
     
  19. ArminKH

    ArminKH Well-Known Member

    i get above error too
    i'm using android 23 to compile my app and i'm failed but there is not any problem when i'm using android 22
    as you said the problem is when i want to use it in android 6
    so as this is a class how is possible to change themes based on ok http?
    which part of code should be changed and what is the ok http code for that point?
    thank u
     
  20. ArminKH

    ArminKH Well-Known Member

    maybe @Erel need to update this class (or add another version) to support OkHttp
     
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