B4J Question JRDC2 - can't get it to work

derez

Expert
Licensed User
Longtime User
I am trying to get it working but there are too many links and I get confused. I use the jrdc2 server example after changing the config.properties file to deal with animals (comment and uncomment the existing code). Used the attached code from another link for b4a client:
Code: Copy
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        reqManager.Initialize(Me, "http://192.168.0.180:54025/rdc")
    End If
    Activity.LoadLayout("1")
'    Dim cmd As DBCommand
'    cmd.Initialize
'    cmd.Name = "create_table"
'    reqManager.ExecuteCommand(cmd,"create")
End Sub

Sub btnSelect_Click
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = "select_animal"
    reqManager.ExecuteQuery(cmd, 0, Null)
End Sub


Sub JobDone(Job As HttpJob)
    If Job.Success = False Then
        Log("Error: " & Job.ErrorMessage)
    Else
        If Job.JobName = "DBRequest" Then
            reqManager.HandleJobAsync(Job, "ReqManager")
        End If
    End If
    Job.Release
End Sub

Sub ReqManager_Result(result As DBResult)
    reqManager.PrintTable(result)
End Sub

There was no table creation code so I added it but I get:
Class not found: b4a.example.main$_dbcommand, trying: b4j.example.main$_dbcommand
Class not found: b4j.example.main$_dbresult, trying: b4a.example.main$_dbresult
in B4j and B4a programs.
I don't understand what is missing.
Is my code correct ?
Can I have one example that is closed on itself with MySQL that works on my PC ?
Thanks.
 
in the server side (jRDC) do you have the "select_animal" command?

"sql.select_animal=SELECT blablablabla" in the config file
 
Upvote 0
Class not found: b4a.example.main$_dbcommand, trying: b4j.example.main$_dbcommand

This is not an error. This is normal for a JRDC2 App and it actually means that the information is being passed between the 2 programs.

most likely your code is correct but the query may not.
 
Upvote 0
in the server side (jRDC) do you have the "select_animal" command?

"sql.select_animal=SELECT blablablabla" in the config file
The config.properties file has this code in it:
Code: Copy
sql.create_table=CREATE TABLE IF NOT EXISTS animals (\
     id INTEGER PRIMARY KEY AUTO_INCREMENT,\
     name CHAR(30) NOT NULL,\
     image BLOB)
sql.insert_animal=INSERT INTO animals VALUES (null, ?,?)
sql.select_animal=SELECT name, image, id FROM animals
 
Last edited:
Upvote 0
Class not found: b4a.example.main$_dbcommand, trying: b4j.example.main$_dbcommand

This is not an error. This is normal for a JRDC2 App and it actually means that the information is being passed between the 2 programs.

most likely your code is correct but the query may not.
Thank you, that is probably correct since I get a good response to the query :
Class not found: b4j.example.main$_dbresult, trying: b4a.example.main$_dbresult
Tag: null, Columns: 3, Rows: 0
name image id

I'll try now to insert data and then with my real DB, see how it goes.
 
Upvote 0
so you are calling a query that does not exist in the config file!
the queries names are created as follows:
sql. - the Server loads all commands that begin with "sql." to a map.
insert_animal - is the name you gave to your query and that you then call in the client side.
the actual query code is set after the equal sign.
 
Upvote 0
so you are calling a query that does not exist in the config file!
the queries names are created as follows:
sql. - the Server loads all commands that begin with "sql." to a map.
insert_animal - is the name you gave to your query and that you then call in the client side.
the actual query code is set after the equal sign.
I guess you saw my first version of the post - which didn't have the last line copied, sorry...

The response in the server is:
Command: query: select_animal, took: 583ms, client=192.168.0.180
 
Upvote 0
so I don't get it! is you data populated? or are you creating an empty table each time you run your app?
what is the result of result.rows.size?
 
Upvote 0
Are you using mariadb?

CREATE TABLE IF NOT EXISTS animals
because this may not work with other databases.

what more could it be... what are you receiving from the B4A side?
 
Upvote 0
Guys, I'm happy with what I have now - the RDC works. The table is not populated yet - it is just the example from Erel. Now I can try to insert data and expand the table to check more. I'll probably come back with more questions later.
 
Upvote 0
Problem came immediately, with insert.
The sql.insert_animal requires two parameters, text and BLOB.
I use this code:
Code: Copy
Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = "insert_animal"
    Dim bmp As Bitmap
    bmp.Initialize(File.DirAssets, "143.jpg")
    cmd.Parameters = Array As Object("Cat",bmp)
    reqManager.ExecuteCommand(cmd,"insert")
I get error message
Error building command: (RuntimeException) java.lang.RuntimeException: Cannot serialize object: android.graphics.Bitmap@52757ea8
It comes from the DBRequestManager class:
Code: Copy
Public Sub ExecuteBatch(ListOfCommands As List, Tag As Object)
    Dim ser As B4XSerializator
    ser.Tag = Tag
    ser.ConvertObjectToBytesAsync(CreateMap("commands": ListOfCommands,  "version": VERSION), "ser")
End Sub

Private Sub ser_ObjectToBytes (Success As Boolean, Bytes() As Byte)
    If Success = False Then
        Log("Error building command: " & LastException)
        Return
    End If
    Dim ser As B4XSerializator = Sender
    SendJob(Bytes, ser.Tag, "batch2")
End Sub

What am I doing wrong ?

Edit: Found the answer here https://www.b4x.com/android/forum/threads/network-asyncstreams-b4xserializator.72149/
 
Last edited:
Upvote 0
Blob = Array of bytes.

Code: Copy
cmd.Parameters = Array ("Cat",reqManager.BitmapToBytes(bmp))

Thank you, it works.
I don't know how to get the blob when I receive result from the DB
Edit:
Found it (also changed the sql to get only one item - sql.select_animal=SELECT name, image, id FROM animals WHERE id=?):
Code: Copy
Sub ReqManager_Result(result As DBResult)
'    reqManager.PrintTable(result)

    If result.Tag = "select" Then
        Dim row() As Object = result.Rows.Get(0)
        Dim image As Bitmap = reqManager.BytesToImage(row(1))
        Dim srcrect, destrect As Rect
        srcrect.Initialize(0,0,image.Width,image.Height)
        destrect.Initialize(0,0,IV.Width,IV.Height)
        Dim cnvs As Canvas
        cnvs.Initialize(IV)
        cnvs.DrawBitmap(image,srcrect,destrect)
    End If
End Sub
 
Last edited:
Upvote 0
Got over this problem by these two small subs:
Code: Copy
Sub TTE(st As String) As String
    Dim nst As String = ""
    For i = 0 To st.Length - 1
        nst = nst & Chr(Asc(st.CharAt(i))- 1423)
    Next
    Return nst
End Sub

Sub TTH(st As String) As String
    Dim nst As String = ""
    For i = 0 To st.Length - 1
        nst = nst & Chr(Asc(st.CharAt(i))+ 1423)
    Next
    Return nst
End Sub

Shifting each Hebrew string to Latin characters before storing it and back when getting the data from the DB.

This works if all the letters are in Hebrew (no spaces etc.) . More general sub is in the way, for mixed text.
 
Last edited:
Upvote 0
Here is the general text shifting sub, to enable Hebrew text to be included in RDC DB even when it is mixed with non-Hebrew text.
Encoding Hebrew to insert to DB:
Code: Copy
Sub TTE(st As String) As String
    Dim nst As String = ""
    For i = 0 To st.Length - 1
        If Asc(st.CharAt(i)) < 1488 Then
            nst = nst & "%" & st.CharAt(i)
        Else
            nst = nst & Chr(Asc(st.CharAt(i))- 1423)
        End If
    Next
    Log(nst)
    Return nst
End Sub

Decoding what you get from the DB:
Code: Copy
Sub TTH(st As String) As String
    Dim nst As String = ""
    Dim flag As Boolean = False
    For i = 0 To st.Length - 1
        If flag Then
            flag = False
            nst = nst &  st.CharAt(i)
        Else
            If st.CharAt(i) = "%" Then
                flag = True
            Else
                nst = nst & Chr(Asc(st.CharAt(i))+ 1423)
            End If
        End If
    Next
    Log(nst)
    Return nst
End Sub
 
Upvote 0
This is not the correct solution. You need to make sure that the database and the tables collate is set to UTF8 and everything will work.

Thank you !
I use this line : JdbcUrl=jdbc:mysql://localhost/test?characterEncoding=utf8
I added this
Code: Copy
CHARACTER SET utf8
COLLATE utf8_universal_ci )
to the end of the table create command but it is still not working.
(also just utf8 or utf8_general_ci)

BTW - I got the idea for the above from you https://www.b4x.com/android/forum/t...results-i-cant-read-hebrew.27461/#post-159533
 
Last edited:
Upvote 0
Use a tool such as phpmyadmin and change the database collate as well.

I've just tested it with hebrew and it does work:

SS-2017-03-13_09.32.54.png


SS-2017-03-13_09.33.27.png


SS-2017-03-13_09.33.27.png


SS-2017-03-13_09.34.45.png
 
Upvote 0
You can also use Heidi. It lets you connect to your remote database, do queries and see the commands being issied
 
Upvote 0
Back
Top