Android Question Android Http Server Error When Posting NameValuePair from VB.NET

hatzisn

Expert
Licensed User
Longtime User
This post refers to the Android HTTP Server as seen in the following link:
https://www.b4x.com/android/forum/threads/embed-an-http-server-in-your-android-application.25984/

I tried using the http server by sending with POST a name value pair from vb.net (WinForms app) with the following code:

(the sValue variable length is about 3850 characters)

B4X:
                    If sInfServ = "" Then
                sTempInfServ = InputBox("""Device address"" reported by http server:", "Insert IP", "")
                sTempInfServ = sTempInfServ.Replace(",", ".")
            Else
                sTempInfServ = sInfServ
            End If

            Dim nv As New NameValueCollection
            Dim sValue As String = Uri.EscapeDataString(sJson)

            nv.Add("json", sValue)

            Using webcl As New WebClient
                Try
                    Dim b() As Byte
                    b = webcl.UploadValues("http://" & sTempInfServ & ":5556/nvget/", nv)
                    MsgBox(System.Text.Encoding.UTF8.GetString(b))

                    sInfServ = sTempInfServ
                Catch ex As Exception
                    MsgBox("Internal error: " & ex.Message)
                    sInfServ = ""
                End Try
            End Using


The code in the 'http server' project in B4A is:

B4X:
Sub HandleNVGET (Request As ServletRequest, Response As ServletResponse)
   
    Try
   
        Dim sJson As String = DecodePath(Request.GetParameter("json"))
   
        Dim jp As JSONParser
        jp.Initialize(sJson)
        Dim l As List
        l.Initialize
        l = jp.NextArray
        .
        .
        .
        .
        .

    Catch
       Log(LastException)
       Response.SendString("Received the data but an error occured.")
    End Try

End Sub


When I compile the b4a project in debug mode everything works perfect. When I turn the compile mode in 'Release (obfuscated)' I get the error in the VB.NET app:
"The request was aborted: The request was canceled."

The error seen on the b4a side in logs is :"(NetworkOnMainThreadException) android.os.NetworkOnMainThreadException"

The errors as seen above are exactly what I get and there are not any more info on them in order to guide me search in the internet for a solution.

Any Ideas and suggestions on this?

Thanks
 

hatzisn

Expert
Licensed User
Longtime User
I do not understand. The server runs on a service which is just started and stopped in Main Activity. Is it considered the Main Thread? When the service receives the data it recreates the json and parses it as seen in the code above and starts a second activity. Could this be the problem? If it is considered the Main thread, when I compile in debug mode does the code added to allow me to debug my code, run my code in a seperate thread and does not raise this error? (I suppose the last question should be answered by Erel...)
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Well, can someone enlighten me? Is a web service considered main thread? I would also like to add that with a get request (with small data size only) everything works perfect... Anyway, I 've managed to do what I wanted in a different way by creating a tcp communications class in either side but I am really wondering why is this happening with the http server lib?
 
Last edited:
Upvote 0
Top