Android Question HttpJob problem

cwt

Active Member
Licensed User
Longtime User
Below is my code for POSTing to an API

B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    j.GetRequest.SetContentType("text/xml")
    j.PostString(Main.ServerURL,PostString)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        xml_parser.Initialize
        xml_parser.Parse(j.GetInputStream, "Parser")
    Else
        xui.MsgboxAsync("Server Connection Error: " & j.ErrorMessage , "Error")
    End If
    j.Release

This code works perfectly for me in B4i but not in B4A. I get the below runtime error message:

java.lang.RuntimeException: Only Post / Put requests support this method.

The error points to this line: j.GetRequest.SetContentType("text/xml")

What do I need to change? Thanks.
 

cwt

Active Member
Licensed User
Longtime User
I actually did have the correct order in the code. I switched to OkHttpUtils2 from HttpUtils2 and it fixed it.
 
Upvote 0

cwt

Active Member
Licensed User
Longtime User
Still have problems.

If I use the Http andHttpUtils2 libraries I get this error message, which crashes the app on runtime:

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/methods/HttpPost; - I am trying to connect to an SSL server but I have added HU2_ACCEPTALL to Build Configurations Conditional Symbols.

If I use the OkHttpUtils2 libraries I get this error message on larger downloads from the server, which does not crash the app:

java.ioEOFException: source exhaused prematurely
 
Last edited:
Upvote 0

cwt

Active Member
Licensed User
Longtime User
For anyone out there who is getting this error while downloading from an API:

java.ioEOFException: source exhaused prematurely

This is normally caused by a compression-decompression issue and can usually be fixed with adding the header line shown below:

j.GetRequest.SetHeader("Accept-Encoding","identity")
B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString(Main.ServerURL,PostString)
    j.GetRequest.SetContentType("text/xml")
    j.GetRequest.SetHeader("Accept-Encoding","identity")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'parse your data here
    Else
       'error message here
    End If
    j.Release

This is less than a rare problem with OkHttpUtils2 - if you write your own server-side APIs as I do then it is not too hard to track down but if you rely on someone else for the server APIs this can be a killer problem.
 
Upvote 0
Top