Android Question Casting an object?

Kevin Golding

Member
Licensed User
Longtime User
Hello,

I need to pass a ServletResponse object as a Tag in an HttpJob, but I'm not sure how to cast the Tag back to a ServletResponse. Current I'm getting an error saying method SendString not found.

B4X:
Sub Process_Globals
    Private SRV As HttpServer   
End Sub

Sub Service_Create
    SRV.Initialize("SRV")
    SRV.Start(8282)
    Log("HTTP Server started on port 8282")
End Sub

Sub SRV_HandleRequest(Request As ServletRequest, Response As ServletResponse)
    Log(Request.Method)
    Log(Request.RequestURI)
   
    Dim j As HttpJob
    j.Initialize("HttpJob", Me)
    If Request.Method = "GET" Then
        j.Tag = Response
        j.Download("http://8.8.8.8.8:8282/" & Request.RequestURI)
    End If
End Sub

Sub JobDone(Job As HttpJob)
    Dim Response As ServletResponse = Job.Tag
    Response.SendString(Job.GetString) ` THIS IS WERE I'M GETTING THE ERROR
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Kevin Golding

Member
Licensed User
Longtime User
Your code looks correct (though you must check that Job.Success is true).
What is the output of:
B4X:
Log(Job.Tag)
Log(GetType(Job.Tag))

I've changed my code to:

B4X:
Sub JobDone(Job As HttpJob)
    Log("Job: " & Job.tag)
    Log("Job: " & GetType(Job.tag))
    Dim Response As ServletResponse = Job.Tag
    Log("Response: " & Response)
    Log("Response: " & GetType(Response))
    If Job.Success Then
        Response.SendString("SUCCESS") ' ERROR HAPPENS HERE
    Else
        Response.Status = 400
    End If
End Sub

The debug shows:
B4X:
Job: anywheresoftware.b4a.objects.Servlet$ServletResponseWrapper@a1d7647
Job: anywheresoftware.b4a.objects.Servlet$ServletResponseWrapper
Response: anywheresoftware.b4a.objects.Servlet$ServletResponseWrapper@a1d7647
Response: anywheresoftware.b4a.objects.Servlet$ServletResponseWrapper
Error occurred on line: 52 (REST_Proxy)
java.lang.RuntimeException: Method: SendString not found in: java.lang.Object
    at anywheresoftware.b4a.shell.Shell$MethodCache.getMethod(Shell.java:968)
    at anywheresoftware.b4a.shell.Shell.getMethod(Shell.java:621)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:750)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA$2.run(BA.java:328)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 
Upvote 0

Kevin Golding

Member
Licensed User
Longtime User
What happens in Release mode?

The app doesn't produce an error and continues to run, but my response does not get to the browser. I just get a blank page but I do get a response as the browser doesn't show an error or timeout.
 
Upvote 0

Kevin Golding

Member
Licensed User
Longtime User
The problem is that the response is committed at the end of the HandleRequest event. You cannot later send anything. This means that you cannot send a http request at that point.

Consider implementing a real http server with B4J. It is much more powerful.

But I need to do this on Android. Will I suffer the same if I do it using a TCP socket server on the inbound request?
 
Upvote 0
Top