Android Question How to download text in a file.txt from a server

ciginfo

Well-Known Member
Licensed User
Longtime User
Hello,
With OKHttputilis2 I want to download text in a file.txt from a server and put it into a variable.
I Know to download an image and I use this Sub which runs fine
B4X:
Sub DownloadImage(Link As String, iv As ImageView)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        iv.Bitmap = j.GetBitmap
    End If
    j.Release
End Sub
How to modify this sub to download text that is in a file.txt on the server ant put it into a string variable
The sub below does'nt run.
Thank you
B4X:
Sub DownloadString(Link As String, TheString As String )
Dim j As HttpJob
j.Initialize("", Me)
j.Download(Link)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
    TheString = j.GetString
   Log(j.GetString)
End If
j.Release
End Sub
 

aeric

Expert
Licensed User
Longtime User
I think this code snippet helps.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim URL As String = "https://filesamples.com/samples/document/txt/sample1.txt"
    Wait For (DownloadTxt(URL)) Complete (Content As String)
    Log(Content)
End Sub

Sub DownloadTxt (Url As String) As ResumableSub
    Dim TheString As String
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Url)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        TheString = j.GetString
    End If
    j.Release
    Return TheString
End Sub
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim URL As String = "https://filesamples.com/samples/document/txt/sample1.txt"
    Wait For (DownloadTxt(URL)) Complete (Content As String)
    Log(Content)
End Sub

Sub DownloadTxt (Url As String) As ResumableSub
    Dim TheString As String
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Url)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        TheString = j.GetString
    End If
    j.Release
    Return TheString
End Sub
OK, Thank you, it runs fine.
 
Upvote 0
Top