Dear sir,
When trying to download Buffer file with extension .BUF, got an error , "File not found". But that file is present in Temp folder of Web server.
My code is
Main Activity
DownloadService File
Please help me.
When trying to download Buffer file with extension .BUF, got an error , "File not found". But that file is present in Temp folder of Web server.
My code is
Main Activity
B4X:
Public Sub dwnLoadFile1()
Dim dd As DownloadData
dd.url = "http://prc.btmp.in/Temp/BUSY_17071000_BS_17 to SS_17.BUF"
dd.EventName = "dd"
dd.Target = Me
CallSubDelayed2(DownloadService, "StartDownload", dd)
End Sub
Sub dd_Complete(Job As HttpJob)
If Job.Success Then
lblProgress.Text="Download completed"
size=Job.GetInputStream.BytesAvailable
Log(size)
File.MakeDir(File.DirDefaultExternal, "Busy")
Dim out As OutputStream = File.OpenOutput(File.DirDefaultExternal & "/Busy/", FileName, False)
File.Copy2(Job.GetInputStream, out)
Dim Buffer() As Byte 'declares an empty array
out.InitializeToBytesArray(size)
Buffer = out.ToBytesArray
out.WriteBytes(Buffer,0,Buffer.Length)
out.Close
Else
Log("Error: " & Job.ErrorMessage)
End If
Job.Release
End Sub
DownloadService File
B4X:
#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
Private jobs As Map
Private timer1 As Timer
Type DownloadData (url As String, Target As Object, EventName As String)
Type JobTag (Data As DownloadData, _
CountingStream As CountingOutputStream, Total As Long)
Private pw As PhoneWakeState
End Sub
Sub Service_Create
jobs.Initialize
timer1.Initialize("timer1", 1000)
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub Service_Destroy
End Sub
Private Sub StartTimer (Target As Object)
Dim n As Notification
n.Initialize
n.Icon = "icon"
n.Vibrate = False
n.Sound = False
n.Light = False
n.SetInfo("Downloading file...", "", Target)
Service.StartForeground(1, n)
timer1.Enabled = True
pw.PartialLock
End Sub
Private Sub EndTimer
Service.StopForeground(1)
timer1.Enabled = False
pw.ReleasePartialLock
End Sub
Public Sub StartDownload(data As DownloadData)
If jobs.ContainsKey(data.url) Then
Log("Ignoring duplicate request.")
Return
End If
Dim J As HttpJob
J.Initialize(data.url, Me)
Dim tag As JobTag
tag.Initialize
tag.data = data
J.tag = tag
jobs.Put(data.url, J)
J.Download(data.url)
If timer1.Enabled = False Then StartTimer(data.Target)
End Sub
Public Sub CancelDownload(url As String)
If jobs.ContainsKey(url) = False Then
Log("Ignoring cancel request.")
Return
End If
Dim job As HttpJob = jobs.Get(url)
Dim jt As JobTag = job.Tag
If jt.CountingStream.IsInitialized Then
jt.CountingStream.Close
Else
jt.Data.url = ""
End If
End Sub
Sub timer1_tick
For Each job As HttpJob In jobs.Values
Dim jt As JobTag = job.Tag
If jt.CountingStream.IsInitialized Then
CallSub3(jt.Data.Target, jt.Data.EventName & "_Progress", _
jt.CountingStream.Count, jt.Total)
End If
Next
End Sub
Sub JobDone(job As HttpJob)
jobs.Remove(job.JobName)
Dim jt As JobTag = job.Tag
If jobs.Size = 0 Then EndTimer
If job.Success Then
CallSubDelayed3(jt.Data.Target, jt.Data.EventName & "_Progress", _
jt.CountingStream.Count, jt.Total)
CallSubDelayed2(jt.Data.Target, jt.Data.EventName & "_Complete", _
job)
Else
Log(job.ErrorMessage)
CallSubDelayed2(jt.Data.Target, jt.Data.EventName & "_Complete", _
job)
End If
End Sub
Please help me.
Last edited: