Downloading Files from Web Server

Bill Norris

Active Member
Licensed User
Longtime User
I've been using the following code to download one file from web server. The value of FileUrl is set with the appropriate file I am downloading. Is possible to send a list of files to download, or is necessary to do them individually, by changing the value of FileUrl for each file.

B4X:
Sub get_new_data
   HttpUtils.CallbackActivity = "Main" 'Current activity name.
    HttpUtils.CallbackJobDoneSub = "JobDone"
    HttpUtils.Download("Job1", FileUrl)
End Sub
   
Sub JobDone (Job As String)
   If HttpUtils.IsSuccess(FileUrl) Then 
File.Copy2(HttpUtils.GetInputStream(FileUrl),File.OpenOutput(File.DirDefaultExternal,"filename",False))
   End If
End Sub
 

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
urgent help needed...

:sign0085::sign0085:
hi,
i'm trying to download a text file from a website using the above sample
the result is an empty file
what i get is the file in the right place but no content - it is empty

i'm using this exact sample, just changed the file name and url, naturally

alternatively - can any one help with a good & simple example on how to directly download a text file from a url ?

this is really urgent for me and i do appreciate any help

thanks
 
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

try inserting this line after the file.copy2 line

B4X:
httputils.GetInputStream.close


I had a similar problem when I first started with this but Erel got me on the right track.
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
oh god, oh silly me...
i solved it myself by just reading what i have done
thank you all for at least trying
thanks
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
another question - if i may...

guys,

since you've been so kind & helpful, i have another question if i may:

how do i trap the back key so it wont exit the app but i will be able to show a message or page or anything ekse ?

secondly how do i trap errors ?
in vb.net i have try or vb6 there is on error - what is the procedure in b4a ?

thanks alot...
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
To catch the back key
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean  ' Return true to consume the event
   If KeyCode = KeyCodes.KEYCODE_BACK  Then  ' Back button
        'your code here
      Return True  ' Consume event, so the app does not close
   Else
       Return False ' Do not consume, the app will close or go to previous activity
   End If
End Sub

For error catching (from the B4A HelpViewer which is indispensable)
B4X:
Exception
Holds a thrown exception.
You can access the last thrown exception by calling LastException.
For example:
Try
   Dim in As InputStream
   in = File.OpenInput(File.DirInternal, "SomeMissingFile.txt")
   '...
Catch
   Log(LastException.Message)
End Try
If in.IsInitialized Then in.Close
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
OMG - guys you are all saints :)
you are saving me a lot of time and effort drilling down forums and web sites
this is priceless!!
thanks once again
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
ok - now it the real hard nut...

guys - a real hard nut for you -

anyone has any info on how to use a printer ?
i mean - i have a samsung bixolon spp-200 - it's a thermal printer with bluetooth card reader.
anyone has any experience with such a nut ?

thansk
 
Upvote 0
Top