Android Question Help for httputils2, json and connection for api over http.

frankinator

Member
Licensed User
Longtime User
Hi,
I'm really struggling with trying to get this to work. No matter what I try I get errors and fail. I'm wondering if i can get an assist. I have read and tried httputils2 but the connection fails or errors. firewall is down and it works successfully with my vb.net code, so i know it's my approach.
What am i missing? Not SSL btw.

Working VB.net code:
B4X:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ' Dim data1 = Encoding.UTF8.GetBytes("{'command': 'summary'}")
        sendCommand("summary")
    End Sub

    Private Sub sendCommand(ByVal command As String)
        Try
            Dim clientSocket As New System.Net.Sockets.TcpClient()
            clientSocket.Connect("192.168.1.72", 4028)
            Dim serverStream As NetworkStream = clientSocket.GetStream()
            Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(command)
            serverStream.Write(outStream, 0, outStream.Length)
            serverStream.Flush()

            Dim inStream As Byte() = New Byte(65536) {}
            serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
            Dim returnData As String = System.Text.Encoding.ASCII.GetString(inStream)
            '  MsgBox(returnData)
            TextBox1.Text = (returnData)
            '  Debug.Print(returnData)
            clientSocket.Close()
        Catch exc As Exception
        End Try
    End Sub

This is just one of my attempts and the error (Error: org.apache.http.client.ClientProtocolException)
I am not sure if I am just approaching this completely wrong.

B4X:
    Sub Senddata   
   
  Dim job2 As HttpJob 
    Dim objMap As Map: objMap.Initialize
   
    'Send a POST request
            job2.Initialize("Job2", Me)
            objMap.Put("command","summary")

            Dim objJSon As JSONGenerator: objJSon.Initialize(objMap)
           
            job2.PostString("HTTP://192.168.1.70:4028", objJSon.ToPrettyString(2))
            job2.GetRequest.SetContentType("application/json")
   
       
    End Sub

At this point I've been trying for a few days and I got nowhere.
Any help or code hints would be greatly appreciated.

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
job2.PostString does send a POST-request to the server.
What serversoftware is on the other side which recieves the data?

Is the destination able to handle html-form-data posted by GET or POST?
 
Upvote 0

frankinator

Member
Licensed User
Longtime User
This is a statement from the docs.

"
it will listen on a
simple TCP/IP socket for single string API requests from the same machine
running cgminer and reply with a string and then close the socket each time
If you add the "--api-network" option, it will accept API requests from any
network attached computer."

It's an application and I'm pretty sure its custom and not really a webserver.
I did try other ways e.g. download, download2 as outlined in some of the samples.

I just wanted to post at least one of my failures as a sample, but I'm open to any possibilities to connect.

In the .net code i posted, it's just hitting a socket not really html.
Again, I'm probably wayoff with my b4a approach, but I don't know what i don't know.

thx
 
Upvote 0
Top