XML Stream

Omar

Member
Licensed User
Longtime User
Hello,

I'm facing some difficulty handling an XML response back from the server and would greatly appreciate any assistance with this.

I have declared and initialized the client I believe correctly under Process Globals:

B4X:
   Dim hc As HttpClient

Then I have my request in my sub as follows:

B4X:
         hc.Initialize("hc")
   
         Dim req As HttpRequest
            req.InitializeGet("http://myserveraddress.com---)
            hc.Execute(req, 1)

And lastly I have my sub to handle the response:

B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   
   Dim Result As InputStream
   Result = Response.GetInputStream
        Log(Result)
        'Work with the Result here
        Result.Close
   
End Sub

Unfortunately I have found that the nothing is coming into Result and nothing is logged either. When I saved the stream into a file it works fine but I cannot seem to be able to handle the stream directly?

Any ideas or help will be greatly appreciated.

Thanks.
 

Kiffi

Well-Known Member
Licensed User
Longtime User
do you have a ResponseError-Sub?
B4X:
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   
   Msgbox(Reason, "Error")

End Sub

Greetings ... Kiffi
 
Upvote 0

Omar

Member
Licensed User
Longtime User
Thanks Kiffi for your quick response.

I did not make the ResponseError sub because the server is returning the data correctly as I can save this into a file, only when I try to work with the stream directly it does nothing.

I will though add this sub right now and try again. Thanks again.


Edit: I've added in the ResponseError sub but no error is reported either.


do you have a ResponseError-Sub?
B4X:
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   
   Msgbox(Reason, "Error")

End Sub

Greetings ... Kiffi
 
Last edited:
Upvote 0

Roeschti

Member
Licensed User
Longtime User
That means your code works as it should.

Have a look at this:

Dim Result As InputStream
Result = Response.GetInputStream

To log you can use Result = Response.GetString and

If you feel ok with Inputstreams maybe something like this could help

B4X:
Dim Reader As TextReader
    Reader.Initialize(Result, "myresponse.txt"))
    Dim line As String
     line = Reader.ReadLine
     Do While line <> Null
        Log(line)
        line = Reader.ReadLine
Reader.Close 
    Loop

I didn't verfy this code, cause i'm outside of my office.
 
Upvote 0

Omar

Member
Licensed User
Longtime User
Thank you Roeschti.

I appreciate your suggestion, I will take a look at my code again because I'm expecting to parse the xml input but seems that there is nothing coming into the inputstream as the code just does nothing afterwards.



That means your code works as it should.

Have a look at this:

Dim Result As InputStream
Result = Response.GetInputStream

To log you can use Result = Response.GetString and

If you feel ok with Inputstreams maybe something like this could help

B4X:
Dim Reader As TextReader
    Reader.Initialize(Result, "myresponse.txt"))
    Dim line As String
     line = Reader.ReadLine
     Do While line <> Null
        Log(line)
        line = Reader.ReadLine
Reader.Close 
    Loop

I didn't verfy this code, cause i'm outside of my office.
 
Upvote 0

Omar

Member
Licensed User
Longtime User
Upvote 0
Top