Android Question Download zip file from web http

vecino

Well-Known Member
Licensed User
Longtime User
Hi, I'm trying to download a zip file from a hosting using http and it shows me the error:
java.lang.IllegalArgumentException: Host name may not be null
I've been looking at several threads on similar topics and I can not find the problem.
This is the code.
B4X:
Sub DownloadZip( cZip As String )  
   Dim j As HttpJob
   j.Initialize("", Me)  
   j.Download( "http://www.mydomain.com/directory/afile.zip" )
   Wait For (j) JobDone(j As HttpJob)  
   If j.Success Then
       Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "filename.dat", False)
       File.Copy2(j.GetInputStream, out)
       out.Close
   Else
       ToastMessageShow("Could not download",False)
   End If
   j.Release
End Sub
Regards.
 

DonManfred

Expert
Licensed User
Longtime User
Post an example code with an REAL URL. In the error there was no hostname (domain.com) in the url.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
And check whether you got permission for DirRootExternal since you use it (there are bettere alternatives; check Erel's video).
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello, thanks friends.
B4X:
Sub DownloadZip( cZip As String )
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download( "http://www.intitec.com/actualizaciones/abc.zip" )
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then   '  It never goes through here !!!  Never Success true
       Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "filename.dat", False)
       File.Copy2(j.GetInputStream, out)
       out.Close
   Else
       ToastMessageShow("Could not download",False)
       Log(j.ErrorMessage)   '  <---  Error message here.
   End If
   j.Release
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It does work as expected...

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim rp As RuntimePermissions
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    wait for Activity_PermissionResult(Permission As String, success As Boolean)
    If success Then
        Log("Permission is set: "&Permission)
        DownloadZip("http://www.intitec.com/actualizaciones/abc.zip")
    End If
End Sub
Sub DownloadZip( cZip As String )
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download( cZip )
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then   '  It never goes through here !!!  Never Success true
        Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "filename.dat", False)
        File.Copy2(j.GetInputStream, out)
        out.Close
        Log("File saved to "&File.Combine(File.DirRootExternal,"filename.dat"))
    Else
        ToastMessageShow("Could not download",False)
        Log(j.ErrorMessage)   '  <---  Error message here.
    End If
    j.Release
End Sub

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Permission is set: android.permission.WRITE_EXTERNAL_STORAGE
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
File saved to /storage/emulated/0/filename.dat
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello again, with your code works fine.
It must be a permission problem when writing.
Thank you very much, I do not know what I would do without you :)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It must be a permission problem when writing.
Use Runtimepermissions and make sure to have permission before downloading anything.

This is not a new fact (Permission needed)! It is needed since api 23 and we are now at 28.
 
Upvote 0
Top