B4J Question cloudkvs users list from server

jcohedman

Member
Licensed User
Longtime User
I want to know if it's possible to get server's user list from client, using cloudKvs.
Thank You.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can add a handler to the server that returns all users and call it with OkHttpUtils2.

Add this code to the DB module:
B4X:
Public Sub GetAllUsers As List
   Dim users As List
   users.Initialize
   Dim rs As ResultSet = sql.ExecQuery("SELECT user FROM data")
   Do While rs.NextRow
     users.Add(rs.GetString("user"))
   Loop
   rs.Close
   Return users
End Sub

Create a new standard handler class named GetAllUsers with this code:
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
   Dim ser As B4XSerializator
   Dim bytes() As Byte = ser.ConvertObjectToBytes(DB.GetAllUsers)
   resp.OutputStream.WriteBytes(bytes, 0, bytes.Length)
End Sub

Register it in the main module:
B4X:
server.AddHandler("/getallusers", "GetAllUsers", False)

Now you can call it from the client with:
B4X:
Dim job As HttpJob
job.Initialize("", Me)
job.Download(ServerUrl & "/getallusers")
Wait For (job) JobDone(job As HttpJob)
If job.Success Then
   Dim ser As B4XSerializator
   Dim users As List = ser.ConvertBytesToObject(Bit.InputStreamToBytes(job.GetInputStream))
   Log(user)
End If
job.Release
 
Upvote 0
Top