Android Question Download big files with HttpJob

Cadenzo

Active Member
Licensed User
Longtime User
Hi, my app downloads large (video)files from a server, running asp.net. The app request tells filename and offset and than gets about 1 MB of data. Than next request with next offset (see code).
It works, but I have the feeling, that it is not the best solution. I have now larger files > 70MB and so I have to call the server 70 times for one file. I think, a B4J server could be better, but I don't own the server by myself and it is only ASP.Net running on it. So my questions are:

1. What would be the max byte array, that I can download that way in one part?
2. Could I improve something else, so may be not always open and close the server every time?

B4X:
Sub StartDownload
    Dim sRequest As String = GetRequest(dlt) 'dlt is a Type with Download-Infos like filename'
    Dim sLokalPfad As String = dlt.filename
    Dim Out As OutputStream
    Out = File.OpenOutput(sFolder, sLokalPfad, True)
        
        Do While(iOffs < iSize) 'Download File in parts
            Dim sRequest As String = GetRequest(dlt)
            Dim job As HttpJob
            job.Initialize("", Me)
            job.PostString(APPSERVER, sRequest) 'Call server
            job.GetRequest.SetContentType("text/plain")
    
            Wait For (job) JobDone(job As HttpJob) 'wait for answer
            If job.Success Then
                
                Dim iBytesDownloaded As Int = Download(job.GetInputStream, Out) 'Create file
                
                If iBytesDownloaded <= 0 Then Exit
                iOffs = iOffs + iBytesDownloaded
            Else
                Exit
            End If
            job.Release
        Loop
        
        Out.Close
    Next
End Sub

'Writes the next  part data to file
Sub Download(strm As InputStream, Out As OutputStream) As Int
    Dim buffer(20000000) As Byte 'max 20 MB , currently in server set to max 1 MB 
    Dim iSize As Int = strm.ReadBytes(buffer, 0, buffer.Length)
    If iSize >= buffer.Length Then Return 0 'zu groß
    If iSize <= 0 Then Return 0

    If iSize < 200 Then 'it's a Text (exception-info), not binary
        Dim sResponseErr As String = BytesToString(buffer, 0, iSize, "UTF-8")
        ToastMessage(sResponseErr, False)
        Return 0
    End If
    
    Out.WriteBytes(buffer, 0, iSize)
    
    Return iSize
End Sub

Thanks for your ideas!
 
Top