B4J Question Server-Client How I get file names into listview by index?

XorAndOr

Active Member
Licensed User
Longtime User
how to get the list of file names and put it in the listview by index in the client?
thanks

server code
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    If req.Method <> "POST" Then
        resp.SendError(500, "method not supported.")
        Return
    End If
    'we need to call req.InputStream before calling GetParameter.
    'Otherwise the stream will be read internally (as the parameter might be in the post body).
    Dim In As InputStream = req.InputStream
    Dim reqType As String = req.GetParameter("type")
    If reqType = "" Then
        resp.SendError(500, "Missing type parameter.")
        Return
    End If
    Select reqType
        
'///////////////////////////////////////////////////////////////////////////       
        
        Case "ListFiles"           
                                    
            For Each FileName As String In File.ListFiles("c:\FilePNG")
                
'                ListView1.Items.Add(FileName) ' <------ this works fine in UI App. For any index of listview I have name of file
                
                resp.Write(FileName & CRLF)    '  ---> send to client
                
            Next           
            
'////////////////////////////////////////////////////////////////////////////           
            
        Case "text"
            Dim tr As TextReader
            tr.Initialize(In)
            Log("Received text message: " & CRLF & tr.ReadAll)
            resp.Write("Messaggio ricevuto ok.")
'////////////////////////////////////////////////////////////////////////////
            
        Case "file"
            Dim name As String = req.GetParameter("name")
            Dim out As OutputStream = File.OpenOutput(Main.filesFolder, name, False)
            File.Copy2(In, out)
            out.Close
            Log("Received file: " & name & ", size=" & File.Size(Main.filesFolder, name))
            resp.Write("File ricevuto ok.")
            
    End Select
End Sub

client code whit job done where I need get list file with index
B4X:
Sub ListaFile
    ListView1.Items.Clear
    
    Dim lista As List
    lista.Initialize
    
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString(link & "?type=ListFiles","")' ---> post to server
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        
        Log(j.GetString)       
        
        ListView1.Items.Add(j.GetString)' <---  here I have all the file names, but on index (0) of listview
        
    Else
        
        Log("Error: " & j.ErrorMessage)
    End If
    j.Release   
End Sub

Log server
B4X:
image14191.PNG
image14193.PNG
image14194.png
image14196.png
image14202.PNG
image14205.PNG
image14191.PNG
image14193.PNG
image14194.png
image14196.png
image14202.PNG
image14205.PNG
 

Daestrum

Expert
Licensed User
Longtime User
Assuming the filenames separated by CRLF you could use
B4X:
Dim filenames() As String = Regex.Split(CRLF,j.getString)

ListView1.Items.AddAll(filenames)
 
Last edited:
Upvote 0
Top