Android Question Problem with httpJob StatusCode

davemorris

Active Member
Licensed User
Longtime User
Hi, Guys
I have a problem with this code
Http code:
        Dim urlString As String = modEposWeb.URL_COMMS_API & "?msgSrcDst=0&centreID=" & myData.centre.centreID & "&customerId=" & customerId
        job.PutString(urlString, tempMsg)
        job.GetRequest.SetContentType("application/json;charset=UTF-8")   
        Wait For (job) JobDone(job As HttpJob)
        If job.Success And job.Response.StatusCode = 200 Then
'            ToastMessageShow("Comms message sent to Web Server.", True)
        Else If job.Success And job.Response.StatusCode = 204 Then
            ToastMessageShow("Message REJECTED - Centre Closed", True)
        Else ' An error of some sort occurred
            Dim errorMsg As String = "An error occurred with the HTTP job: " & job.ErrorMessage
            ToastMessageShow(errorMsg, True)
        End If
        statusCode = job.Response.StatusCode
        job.Release ' Must always be called after the job is complete, to free its resources

Basically, if the API call returns status code = 200 - job.Success = true and job.response.StatusCode = 200 and it works great.
However, if the API call returns status code = 204, then job.Success = false then job.response.StatusCode = -1, and I end up with error message (as it drops thro' to the Else statement).
"An error occurred with the HTTP job: okhttp3.internal.http2.StreamResetException: stream was reset: PROTOCOL_ERROR"

I need to detect a 204 response, can anyone help (Note: Same code snippet works in B4i) - also I have selected OkHttp and OkHttpUtils2 libraries.

Regards
Dave
 

davemorris

Active Member
Licensed User
Longtime User
Hi, Thanks again for the response.
At first I can't believe that a well used language like Java as a bug of this type. However, (I am not a Java man) I did search "Java 204 not working" and the first hit was

https://stackoverflow.com/questions...-not-working-with-204-and-empty-body-response

It is 5 years old (and once I am not a Java man) but does tend to suggest that there a problem with Java.

Perhaps the issue is with the wrapper between Java and B4A .

However, I have managed a work around -
Fortunately, the API I am using will return 200 for good calls and only 204 other valid calls. It will return errors in the 400 range for bad calls. I know 409 works, so I assume all the other 400 range will work.

So (this is the workaround - Don't tell may manager - as I am the Boss that me!)
B4X:
If 200 then
    'great we got a response
ELSE IF 400 to 499 then
    ' we are doing some wrong
ELSE
    ' Got to be a 204 (I hope)
END IF

Sadly, it good for me but not a complete workaround.

Once again, thanks for the help.

I do think we have done this issue to death, so I leave it with someone with more experience to perhaps log this issue.

Kind regards
Dave
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
mostly to death; i see a tail still quivering. java doesn't write itself. http3 is third-party software meant to help internet clog. it has been adopted by, at least, android, and is part of a complicated pipeline. i hope your workaround works
 
Upvote 0
Top