Android Question About jRDC v2

MohammadNew

Active Member
Licensed User
Longtime User
Hello everyone,

I tried jRDC2 in my pc (localhost) first time ...

I put (config and jRDC2) files in folder (firsjRDC2) in C:\ like

locationjrdc2.png

then config file I modify to

config.png

My app has DBRequestManager

B4X:
Sub Class_Globals
    Private mTarget As Object
    Private link As String
    Private VERSION As Float = 2

End Sub

'Target - The module that handles JobDone (usually Me).
'ConnectorLink - URL of the Java server.
Public Sub Initialize (Target As Object, ConnectorLink As String)
    mTarget = Target
    link = ConnectorLink
End Sub

'Sends a query request.
'Command - Query name and parameters.
'Limit - Maximum rows to return or 0 for no limit.
'Tag - An object that will be returned in the result.
Public Sub ExecuteQuery(Command As DBCommand, Limit As Int, Tag As Object)
    Dim ser As B4XSerializator
    Dim data() As Byte = ser.ConvertObjectToBytes(CreateMap("command": Command, "limit": Limit,  "version": VERSION))
    SendJob(data, Tag, "query2")
End Sub

Private Sub SendJob(Data() As Byte, Tag As Object, Method As String)
    Dim j As HttpJob
    j.Initialize("DBRequest", mTarget)
    j.Tag = Tag
    j.PostBytes(link & "?method=" & Method , Data)
End Sub

'Executes a batch of (non-select) commands.
'ListOfCommands - List of the commands that will be executes.
'Tag - An object that will be returned in the result.
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

'Similar to ExecuteBatch. Sends a single command.
Public Sub ExecuteCommand(Command As DBCommand, Tag As Object)
    ExecuteBatch(Array As DBCommand(Command), Tag)
End Sub

'Handles the Job result and returns a DBResult.
'It is recommended to use HandleJobAsync instead.
Public Sub HandleJob(Job As HttpJob) As DBResult
    Dim ser As B4XSerializator
    Dim data() As Byte = Bit.InputStreamToBytes(Job.GetInputStream)
    Dim res As DBResult = ser.ConvertBytesToObject(data)
    res.Tag = Job.Tag
    Return res
End Sub
'Handles the Job result and raises the Result event when the data is ready.

Public Sub HandleJobAsync(Job As HttpJob, EventName As String)
    Dim ser As B4XSerializator
    Dim data() As Byte = Bit.InputStreamToBytes(Job.GetInputStream)
    Dim m As Map = CreateMap("event": EventName)
    If Job.Tag <> Null Then m.Put("tag", Job.Tag) 'this is required because of a bug in B4i v2.31
    ser.Tag = m
    ser.ConvertBytesToObjectAsync(data, "ser")
End Sub

Private Sub ser_BytesToObject (Success As Boolean, NewObject As Object)
    If Success = False Then
        Log("Error reading response: " & LastException)
    End If
    Dim ser As B4XSerializator = Sender
    Dim m As Map = ser.Tag
    Dim res As DBResult = NewObject
    res.Tag = m.Get("tag")
    CallSubDelayed2(mTarget, m.Get("event") & "_result", res)
End Sub



'Reads a file and returns the file as a bytes array.
Public Sub FileToBytes(Dir As String, FileName As String) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    Dim In As InputStream = File.OpenInput(Dir, FileName)
    File.Copy2(In, out)
    out.Close
    Return out.ToBytesArray
End Sub
'Converts an image to a bytes array (for BLOB fields).
Public Sub ImageToBytes(Image As Bitmap) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    Image.WriteToStream(out, 100, "JPEG")
    out.Close
    Return out.ToBytesArray
End Sub
'Converts a bytes array to an image (for BLOB fields).
Public Sub BytesToImage(bytes() As Byte) As Bitmap
    Dim In As InputStream
    In.InitializeFromBytesArray(bytes, 0, bytes.Length)
    Dim bmp As Bitmap
    bmp.Initialize2(In)
    Return bmp
End Sub
'Prints the table to the logs.
Public Sub PrintTable(Table As DBResult)
    Log("Tag: " & Table.Tag & ", Columns: " & Table.Columns.Size & ", Rows: " & Table.Rows.Size)
    Dim sb As StringBuilder
    sb.Initialize
    For Each col In Table.Columns.Keys
        sb.Append(col).Append(TAB)
    Next
    Log(sb.ToString)
    For Each row() As Object In Table.Rows
        Dim sb As StringBuilder
        sb.Initialize
        For Each record As Object In row
            sb.Append(record).Append(TAB)
        Next
        Log(sb.ToString)
    Next
End Sub

Then the code B4A

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim reqManager As DBRequestManager
    Type DBResult (Tag As Object, Columns As Map, Rows As List)
    Type DBCommand (Name As String, Parameters() As Object)
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    
    Private Button1 As Button

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")

    If FirstTime Then
        reqManager.Initialize(Me, "http://127.0.0.1:17178/firsjRDC2")
    End If

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = "selectsinglerow"
    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)
    Dim Record() As Object
    Record = result.Rows.Get(0) ' The First Record,

    ToastMessageShow( Record(result.Columns.Get("id")) , False )

    ToastMessageShow( Record(result.Columns.Get("name")) , False )
End Sub

After that I got error

B4X:
Logger connected to:  samsung SM-A700FD
--------- beginning of main
--------- beginning of system
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
ResponseError. Reason: java.net.ConnectException: Failed to connect to /127.0.0.1:17178, Response:
Error: java.net.ConnectException: Failed to connect to /127.0.0.1:17178
ResponseError. Reason: java.net.ConnectException: Failed to connect to /127.0.0.1:17178, Response:
Error: java.net.ConnectException: Failed to connect to /127.0.0.1:17178
** Activity (main) Pause, UserClosed = false **
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
ResponseError. Reason: java.net.ConnectException: Failed to connect to /127.0.0.1:17178, Response:
Error: java.net.ConnectException: Failed to connect to /127.0.0.1:17178

I hope to I find an answer ... Thanks
 
Last edited:

MohammadNew

Active Member
Licensed User
Longtime User
Thanks Erel, the port opened ,, and I modify the config file to

config.png

I put the PC ip address

the code B4a

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim reqManager As DBRequestManager
    Type DBResult (Tag As Object, Columns As Map, Rows As List)
    Type DBCommand (Name As String, Parameters() As Object)
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
     
    Private Button1 As Button
    Private EditText2 As EditText
    Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")

    If FirstTime Then
        reqManager.Initialize(Me, "http://192.168.1.2:17178/firsjRDC2")
    End If
 
End Sub

Sub Activity_Resume
 
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = "selectsinglerow"
    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)
    Dim Record() As Object
    Record = result.Rows.Get(0) ' The First Record,

    ToastMessageShow( Record(result.Columns.Get("id")) , False )

    ToastMessageShow( Record(result.Columns.Get("name")) , False )
End Sub

I got error :

B4X:
Logger connected to:  samsung SM-A700FD
--------- beginning of main
--------- beginning of system
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
ResponseError. Reason: java.net.ConnectException: Failed to connect to /192.168.1.2:17178, Response:
Error: java.net.ConnectException: Failed to connect to /192.168.1.2:17178


mysqlstart.png
 
Upvote 0

MohammadNew

Active Member
Licensed User
Longtime User
The ip address in the JdbcUrl string should be 127.0.0.1 as RDC and the database server are running on the same computer.
Hi Erel, You mean 127.0.0.1 put it in config file or code B4A ?

1. Is jRDC 2 running?
Really , I do not know if running or no ! how to know Mr. Erel is jRDC 2 running ?

2. Are you able to connect to it from the local PC?
I do not know how to connect to it , this is first time I try it .

Thanks alot
 
Upvote 0

MohammadNew

Active Member
Licensed User
Longtime User
Yes, I put 127.0.0.1 in config file and try to 127.0.0.1:17178/test in browser

erel.png

what is 127.0.0.1:17178/test > test? maybe change it or default .
 
Upvote 0
Top