Android Question PostString to Web Service failing with 500 internal server error

mfstuart

Active Member
Licensed User
Longtime User
Hi all,
This is hopefully the last set of code of my scanning app to get working - posting the scanned data in JSON format back to the web service...

I'm using this code to do the post job, but it is continually failing with 500 internal server error.
Can someone please look it over?
What I'm not sure of is what is the correct way to setup the server to accept POSTS?
All GET's work fine to the same URL.

BTW: I'm in great appreciation of all the submits of code from so many talented people on this forum :)

B4X:
'this is a resumable sub with the Wait For line of code
Sub btnPostJSON_Click
    'get first TOP 20 records for testing
    Dim strSQL As String = "SELECT ScanID, HID, PLU, Description, Qty, ItemPrice, UserID, CreateDateTime FROM ScanDetail WHERE UploadDateTime is Null ORDER BY HID LIMIT 20"
    Dim cur As Cursor
    Dim L1 As List
    Dim M1 As Map
   
    L1.Initialize
    DateTime.DateFormat = "yyyy-MM-dd"
   
    Try
        cur = SQL.ExecQuery(strSQL)
        For i = 0 To cur.RowCount - 1
            cur.Position = i
            M1.Initialize
            M1.Put("hid", cur.GetInt("HID"))
            M1.Put("plu", cur.GetString("PLU"))
            M1.Put("description",cur.GetString("Description"))
            M1.Put("qty", cur.GetInt("Qty"))
            M1.Put("price", cur.GetDouble("ItemPrice"))
            M1.Put("userid", cur.GetString("UserID"))
            M1.Put("scandatetime", DateTime.Date(cur.GetString("CreateDateTime")) & " " & DateTime.Time(cur.GetString("CreateDateTime")))
            L1.Add(M1)
        Next
        Dim gen As JSONGenerator
        gen.Initialize2(L1)
        Dim jsn As String
        jsn = gen.ToPrettyString(2)
       
        Dim HJ As HttpJob
        HJ.Initialize("PostScanDet",Me)
        'we use Alpha Anywhere application for GETS and POSTS as the web service host app,
        'hence the .a5w in the URL
        HJ.PostString("http://ServerNameHere:PortNumberHere/PostJSONData.a5w",jsn)
       
        'Resumable Sub code:
        Wait For (HJ) JobDone(HJ As HttpJob)

        If HJ.Success Then
            Select HJ.JobName
                Case "PostScanDet"
                    Log(HJ.JobName)
                    Dim res As String = HJ.GetString
                    Log(res)
            End Select
        Else
            Log(HJ.ErrorMessage)
        End If
    Catch
        Log(LastException)
    End Try
    cur.Close
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
remove the try catch block and post the error if you get one.
Additionally you forget to release the job at the sub end.

Whether it accept a poststring like you are sending depends on the webserver.
What kind of webserver are you running?
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
remove the try catch block and post the error if you get one.
Additionally you forget to release the job at the sub end.

Whether it accept a poststring like you are sending depends on the webserver.
What kind of webserver are you running?

Web Server = Windows Server 2012
Web Service = Apache
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
remove the try catch block and post the error if you get one.
Additionally you forget to release the job at the sub end.

Whether it accept a poststring like you are sending depends on the webserver.
What kind of webserver are you running?

And here's the job error message returned:

<!DOCTYPE html>
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>500 Internal Server Error</h1>
An unrecoverable error occurred processing this request. The request will be terminated.
<p>
<hr>
<address>Alpha Anywhere Application Server Application Server/12.0 Build/4346-4889 at WSA5 Port 443</address>
</body></html>
Internal Server Error
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
Who is handling the request? PHP, ASP, whatever?
check the server log for errors. 500 is a server-crash.

Hi DonManfred,
The web service is handled by Alpha Anywhere, which is mentioned in the error text above.

Our network guy has been watching with sniffer software on the server and sees the packets coming in, but the data is all garbage.
With that, do you see any lines of code like HJ.PostString to be correct? I was wondering if somehow it's sending garbage 'cause of incorrect coding.

In the long run, we will be creating a .NET solution as the web service running under IIS, but the Alpha/Apache solution is to get this going ASAP. We have all our park locations doing inventory beginning September.

I will ask the network guy for server logs and post those results soon.

Thanx,
Mark S.
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
BTW... I changed up my code by moving the post part of the code out of the TRY/CATCH.
Running the code results in the same error.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I dont know what the server will decode. I don´t know what Anywhere Alpha is or are able to do.
I can´t help on this, sorry.

I guess you need to send any Authorisation-header or so.

Do you have a example call with CURL for ex which works with this server?
Post the curl-Command
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
Hi DonManfred,

I'm not sure what Authorisation-header to type up for the PostString
I don't know what is CURL-command to give an answer to your question.
Looking into this more for you.

Thanx,
Mark S.
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
Do you have an example code (in whichever language) to call this server?
Maybe I'm not understanding you correctly, but...

Client side is the B4A application deployed to a Nexus 7 tablet, which the code is above in my original post. This calls the server, the server being recognized on the network by the tablet as it is "plugged in" with a network cable and adapter.
Server side, the Alpha Anywhere code is in xBasic, a proprietary code of theirs.

HTH,
Mark S.
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
Tried moving
B4X:
Dim M1 As Map
inside the loop?

I think you may have the answer mc73. I will try that on Monday and let you know the results.

Meanwhile, I've taken to changing the code to send each DB record as a PostString with ?name=value?name=value?name=value.
This is working and the Alpha Anywhere web service is successfully consuming each request.
It's a little slower, but it is working, but I'd prefer to send all records via JSON.

Thanx,
Mark S.
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
It is better to put the Dim line inside the loop however it is not related to this issue.

Maybe you need to set the request content type to application/json.
Going about setting the request content is an unknown to me.
Where would I do that and what is the content?

Thanx,
Mark S.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Who wrote the PostData.aw5 script? What does it expect? Just Json? Looking at the docs here (https://www.alphasoftware.com/documentation/pages/Ref/Api/Objects/Server/Context/Request/index.xml), it looks like the POST request needs to be multi-part (see the description of Params), which would exclude the PostString/PostByte/PostFile methods of jHttpUtils2 (someone with more authority/knowledge may chime in on this) and may explain your initial 500 internal error message. It is really hard to help out in a situation where the API of the receiving website is unknown (we are basically fishing here).
 
Upvote 0
Top