Android Question [Solve] No response when call REST over HttpServer

rraswisak

Active Member
Licensed User
Hi all,

I try to implement HttpServer embed on android device (smartphone or tablet) base on this guidance
In the android part i need to do some rest to another server over internet


this is part of my HandleRequest event code:
B4X:
Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
    Try
        Dim p As String = Request.GetParameter("name")
        Log("Client: " & Request.RemoteAddress)
        Select True
            Case Request.RequestURI = "/"
                Response.SendString($"Welcome ${p}"$)
            Case Request.RequestURI = "/hi"
                Response.SendString($"Greeting ${p}, how are you today ?"$)
            Case Request.RequestURI = "/rest"
                Dim j As HttpJob
                Dim res As String
                j.Initialize("", Me)
                j.Download("https://www.google.com")
                Wait For (j) JobDone(j As HttpJob)
                If j.Success Then
                    Log(j.GetString)
                    res = $"${p}, Google.com is ready"$
                Else
                    res = "Error access URL"
                End If
                j.Release
                Response.SendString(res)
            Case Else
                Response.SendString("Invalid command!")
        End Select
    Catch
        Response.Status=500
        Log("Error serving request" & LastException)
        Response.SendString("Invalid command !")
    End Try
End Sub

When i try to call in the browser with:

1587916382756.png


1587916453223.png


The problem is when i run this address, the JobDone status was Success but no result shown (blank) in the browser side

1587916569514.png


Which expected result is: rraswisak, Google.com is ready

What i'm missing ?

Thank you
 

DonManfred

Expert
Licensed User
HttpServer embed on android device
the way the httpserver is implemented the request ends as soon as the sub returns.

You can not use sleep or wait for in the handler-sub.

Whenever Sleep or Wait For are called, the current sub is paused. This is equivalent to calling Return.


Reference:
 
Last edited:
Upvote 0

rraswisak

Active Member
Licensed User
Thank you @DonManfred for pointing me to the right path.

I've been struggling to resolve this problem over three days now, searching about HttpServer including one of thread you just refer. I just not notice @Erel state that calling sleep or wait for will pause the sub or calling return

So i modify the code using old fashion way how to handle JobDone event without using wait for, the result/problem still remain.
I see on okHttpUtil2 source code and CallSubDelayed was fired there on Download method, so i guess this is why my code does not work (terminated).
So my conclusion is calling REST API on HandleRequest event is impossible.

What i'm going to achieve ?
I plan to create android app as a bridge between app on several desktop pc to do some rest call to a web over internet. The desktop pc has NO internet connection, while the android i want to build will connect to WiFi which HAS internet connection. Both desktop pc and android device was manage in the same network by network admin.

The android app will act similar to EDC (Electonic Data Capture) machine with specific job. So using HttpServer is the most simpler i can do to handle desktop app request and make some rest api and send the respond/result to desktop app.

Since its not possible using HttpServer i will use another way to resolve

Thank you
 
Last edited:
Upvote 0
Top