B4J Question Download CSV File

icakinser

Member
Licensed User
Longtime User
When downloading a CSV file using HTTPJOB Is there a way to save a CSV file directly to a string instead of to a file?
B4X:
Sub DownloadAndSave (Url As String, Dir As String, FileName As String) As ResumableSub
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download(Url)
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
       Dim out As OutputStream = File.OpenOutput(Dir, FileName, False)
       File.Copy2(j.GetInputStream, out)
       out.Close
   End If
   j.Release
   Return j.Success
End Sub

How do I convert the j.GetInputStream to just a normal string?
 

drgottjr

Expert
Licensed User
Longtime User
use dim mynormalstring = j.getstring
instead of j.getinputstream (in other words, kill lines 7,8,9

(this assumes utf8. you'd use j.getstring2 if the input is in a different character set)

note: you're only testing for success. that leaves you open to a surprise in the event j.success is false. you should handle that possibility.
 
Upvote 0

icakinser

Member
Licensed User
Longtime User
Just for those that are wondering:
B4X:
If j.Success Then
    'work with result
        Dim csv_buffer As String = j.getstring
    'Log(j.GetString)
               
        table_test = csv_parser.Parse(csv_buffer,";",False)
       
        Dim i As Int
        For i=1 To table_test.Size
            Dim row() As String = table_test.Get(table_test.Size-i)
            'Log(table_test.Size)
           
            Log(row(1))
            Log(row(2))
            Log(row(3))
            Log(row(4))
            Log(row(5))

        Next

Uses the CSVParser class!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…