B4J Question [Server] Send and receive objects Fail

pmt

Member
Licensed User
Hi,

I follow the step from https://www.b4x.com/android/forum/threads/server-send-and-receive-objects.37206/ to learn about Send and receive objects.

But I get error , I tried to fix by myself many times but I can't.

This is B4J Error:

B4X:
java.lang.RuntimeException: java.lang.ClassNotFoundException: b4j.example.actmembernew$_order
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:119)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
    at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:130)
    at anywheresoftware.b4j.object.JServlet.Handle(JServlet.java:110)
    at anywheresoftware.b4j.object.JServlet.doPost(JServlet.java:80)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:841)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:535)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:126)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.Server.handle(Server.java:564)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
    at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
    at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:126)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:673)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:591)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: b4j.example.actmembernew$_order
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readTypeClass(RandomAccessFile.java:525)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readType(RandomAccessFile.java:533)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readObject(RandomAccessFile.java:471)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readList(RandomAccessFile.java:620)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readObject(RandomAccessFile.java:462)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readHelper(RandomAccessFile.java:408)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.ReadObject(RandomAccessFile.java:369)
    at b4j.example.applicationhandler._readobject(applicationhandler.java:123)
    at b4j.example.applicationhandler._handle(applicationhandler.java:89)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    ... 33 more


and this B4A Error:

B4X:
Menu_Click:Done selected
ResponseError. Reason: java.lang.RuntimeException: java.lang.ClassNotFoundException: b4j.example.actmembernew$_order, Response: <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 500 java.lang.RuntimeException: java.lang.ClassNotFoundException: b4j.example.actmembernew$_order</title>
</head>
<body><h2>HTTP ERROR 500</h2>
<p>Problem accessing /addapplication. Reason:
<pre>    java.lang.RuntimeException: java.lang.ClassNotFoundException: b4j.example.actmembernew$_order</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a><hr/>
</body>
</html>
Job done>java.lang.Object@9557b75
Error: <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 500 java.lang.RuntimeException: java.lang.ClassNotFoundException: b4j.example.actmembernew$_order</title>
</head>
<body><h2>HTTP ERROR 500</h2>
<p>Problem accessing /addapplication. Reason:
<pre>    java.lang.RuntimeException: java.lang.ClassNotFoundException: b4j.example.actmembernew$_order</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a><hr/>
</body>
</html>


How to fix this problem?

( I use B4J 5.9 ,B4A 7.01 )


Thank you in advance.

Regards,
pmt
 

pmt

Member
Licensed User
Here some my source code.

Client Code:

B4X:
Sub DoSendOrder
    Dim Orders As List
    Orders.Initialize
    For i = 1 To 20
        Dim o As Order
        o.Initialize
        o.ProductKey = "Item #" & i
        o.Amount = 7
        o.Comment = "abcdef"
        Orders.Add(o)
    Next
    SendObject(Orders)
End Sub

Sub SendObject (Obj As Object)
    raf.WriteObject(Obj, True, 0)
    Dim size As Int = raf.CurrentPosition
    Dim data(size) As Byte
    raf.CurrentPosition = 0
    Do While raf.CurrentPosition < size
        raf.ReadBytes(data, raf.CurrentPosition, size - raf.CurrentPosition, _
       raf.CurrentPosition)
    Loop
 
    Dim j As HttpJob
    j.Initialize("AddApplication", Me)
    j.PostBytes(link, data)
End Sub

Server Code:
B4X:
Sub AppStart (Args() As String)
    srvr.Initialize("")
    rdcConnector1.Initialize
    srvr.Port = rdcConnector1.serverPort
    srvr.AddHandler("/test", "TestHandler", False)
    srvr.AddHandler("/rdc", "RDCHandler", False)
    srvr.AddHandler("/addapplication", "ApplicationHandler", False)
   
    srvr.Start
    Log($"APPServive is running (version = $1.2{VERSION})"$)
    StartMessageLoop
End Sub

ApplicationHandler module:

B4X:
Sub Class_Globals
   
    Type Order(ProductKey As String, Amount As Int, Comment As String)
    Type OrderResult(NumberOfOrders As Int, Success As Boolean)
     Private bufferSize As Int = 1000
    
End Sub

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim orders As List = ReadObject(req.InputStream)
    Dim result As OrderResult
    result.Initialize
    For Each o As Order In orders
        'work with the order
        Log(o.ProductKey & ", " & o.Amount & ", " & o.Comment)
        result.NumberOfOrders = result.NumberOfOrders + 1
    Next
    result.Success = True
    SendObject(result, resp.OutputStream)
End Sub


Regards,
pmt
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sub Class_Globals

Type Order(ProductKey As String, Amount As Int, Comment As String)
Type OrderResult(NumberOfOrders As Int, Success As Boolean)
Private bufferSize As Int = 1000

custom types must be declared in the MAIN module....
 
Upvote 0

pmt

Member
Licensed User
custom types must be declared in the MAIN module....

Worked , Thank you very much DonManfred.

Again , Is it possipble to send the object ( not custom type) from B4A to B4J Server ?

For example:
I have a class Customer

B4X:
Sub Class_Globals
   
    Public txtName As String
    Public txtSurname As String
   
   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

End Sub

I created standard Customer Class module in both B4A side and B4J side , but I don't know how to do as you said "custom types must be declared in the MAIN module".

Regards,
pmt
 
Upvote 0
Top