Android Question HttpJob returns empty InputStream from Job.GetInputStream

ostau

Member
Licensed User
Longtime User
Hallo there,
I want to use the solution with HttpJob and HttpUtils2Service, which is provided as an example application by Erel.
I built a .Net-Webservice, Parameters are supplied by POST-Method.
It returns a XML-String, which will parsed with SAX-Parser.

Here ist the snippet of the modified sample code:

....
WebService_URL = "http://192.168.80.22/PMDSService/Service.asmx/getZusatzeinträge"
WebService_Parameters = "IDPflegeplan=xx"
Dim job1 As HttpJob
job1.Initialize("MyJob", Me)
job1.PostString(WebService_URL, WebService_Parameters)

End Sub


Sub JobDone (Job As HttpJob)

If Job.Success = True Then
Dim x As String
x = Job.GetString

Dim xx As InputStream
xx = Job.GetInputStream

.....

The result as string is correct, the InputStream is empty.

Any ideas, what could be wrong?

Kind regards, Oskar
 

ostau

Member
Licensed User
Longtime User
Hallo Erel,

thank you for your quick response.

1. I set a breakpoint and watched the variable xx. The buffer is empty (all bytes are 0, see attached screenshot).
2. The parser, which gets the result from the Service returns null.
 

Attachments

  • EmptyBuffer.jpg
    EmptyBuffer.jpg
    40.9 KB · Views: 264
Upvote 0

sorex

Expert
Licensed User
Longtime User
is there a reason to use the stream and not just the response text?

just add those values to the json structure.
 
Upvote 0

ostau

Member
Licensed User
Longtime User
I have to pass the result to Sax-Parser and the parser expects an input stream and not a string.
 
Upvote 0

ostau

Member
Licensed User
Longtime User
Seems to be very unlikly. Here is the code of the HttpJob-Class form Erel.

<code>
Public Sub GetInputStream As InputStream
Dim In As InputStream
In = File.OpenInput(HttpUtils2Service.TempFolder, taskId)
Return In
End Sub
</code>


GetString uses the same mechanism (which works)

<code>
Public Sub GetString2(Encoding As String) As String
Dim tr As TextReader
tr.Initialize2(File.OpenInput(HttpUtils2Service.TempFolder, taskId), Encoding)
Dim res As String
res = tr.ReadAll
tr.Close
Return res
End Sub
</code>
 
Upvote 0

ostau

Member
Licensed User
Longtime User
Seems to be very unlikly. Here is the code of the HttpJob-Class form Erel.

B4X:
Public Sub GetInputStream As InputStream
Dim In As InputStream
In = File.OpenInput(HttpUtils2Service.TempFolder, taskId)
Return In
End Sub


GetString uses the same mechanism (which works)

B4X:
Public Sub GetString2(Encoding As String) As String
Dim tr As TextReader
tr.Initialize2(File.OpenInput(HttpUtils2Service.TempFolder, taskId), Encoding)
Dim res As String
res = tr.ReadAll
tr.Close
Return res
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I have to pass the result to Sax-Parser and the parser expects an input stream and not a string.

are you using the right commands?

I always worked with the response text for xml or json parsing.
 
Upvote 0

ostau

Member
Licensed User
Longtime User
are you using the right commands?

I always worked with the response text for xml or json parsing.

Here is my code (now with correct tags:))
B4X:
Dim b() As Byte
b = Job.GetString2("ISO-8859-1").GetBytes("UTF8")
Dim inStream As InputStream
inStream.InitializeFromBytesArray(b, 0, b.Length)
parser.Initialize
CallbackActivity = Job.JobName
parser.Parse (inStream, "Parser")

The only allowed type for the first Parameter of the Sax-Parser is an InputStream. How could I pass a string?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
apparently you can't but when things get more demanding the XOM one will make life a lot easier without the need to write custom parsing checks in that parse sub.
 
Upvote 0

ostau

Member
Licensed User
Longtime User
Sorry guys and thanks for all your help. The problem was caused - as almost always - from the subject in front of the screeen :confused:
The GetInputStream-Method works well, but the content of the stream is available in the debugger AFTER setting the position to the end of the stream.
And the reason, why I didn't get any result from the parser was a typo in the call.
 
Upvote 0

ostau

Member
Licensed User
Longtime User
apparently you can't but when things get more demanding the XOM one will make life a lot easier without the need to write custom parsing checks in that parse sub.
Hallo Sorex,

I had a closer look to your parser and it seems to be exactly what I Need to make things less complex.
But I do have one problem. I can't get things working.
I do have a Sub, which creates the XOMBuilder
B4X:
Dim xml As String
xml = "<dsPMDS xmlns=""http://tempuri.org/dsPMDS.xsd"">"
xml = xml & "<BenutzerShort><Name>Sander admin</Name><Passwort>3FABD40C9AF03343E4CFDB5D4B68A8ED</Passwort><ID>9d88547f-e1f0-402e-a9f4-a6c25c827de8</ID><Kürzel>admin</Kürzel><IDBerufsstand>e969d3f5-f3ca-4c34-a4c9-b65b380601aa</IDBerufsstand></BenutzerShort>"
xml = xml & "<BenutzerShort><Name>Saglik Astrid</Name><Passwort>821D5311AA0374AACF78980388308421</Passwort><ID>1e58709d-aea4-419a-9a76-34241f4452ce</ID><Kürzel>aj</Kürzel><IDBerufsstand>00000000-0000-0000-0002-000000000001</IDBerufsstand></BenutzerShort>"
xml = xml & "<BenutzerShort><Name>Saalbach Brigitte</Name><Passwort>823EAE0D794A154B6D12B53E1296A627</Passwort><ID>1b6e88cd-6ff3-4531-800b-0af324dcab63</ID><Kürzel>bs</Kürzel><IDBerufsstand>acfb447f-d853-4701-88c7-d20bd31ffb31</IDBerufsstand></BenutzerShort>"
xml = xml & "</dsPMDS>"
Dim XOMBuilder1 As XOMBuilder
XOMBuilder1.Initialize("XOMBuilder1")
XOMBuilder1.BuildFromString(xml, "", Null)

The Parser is in the same activity
B4X:
Sub XOMBuilder1_BuildDone(XOMDocument1 As XOMDocument, Tag As Object)
'Gibt die Tabelle als List of Maps zurück
   Dim xmlRows As XOMElements
   Dim xmlRow As XOMElement
   Dim xmlColumns As XOMElements
   Dim xmlColumn As XOMElement
   Dim listFromServiceUser As List
   listFromServiceUser.Initialize
   Dim m As Map
   m.Initialize
   If XOMDocument1.IsInitialized Then
      Log("XOMDocument is initialized")
      Dim RootElement As XOMElement
      RootElement=XOMDocument1.RootElement
      xmlRows = RootElement.GetChildElements
      Dim i, RecordCount As Int
      RecordCount = xmlRows.Size
      For i = 0 To RecordCount - 1
         m.Initialize
         xmlRow = xmlRows.GetElement(i)
         xmlColumns = xmlRow.GetChildElements
         For j = 0 To xmlColumns.Size - 1
            xmlColumn = xmlColumns.GetElement(j)
            Log(xmlColumn.LocalName & " = " & xmlColumn.Value)
            m.Put(xmlColumn.LocalName, xmlColumn.Value)
         Next
         listFromServiceUser.Add(m)
      Next

   Else
      ' XOMDocument1 will be uninitialized if an error has occurred
      Log("An error has occured and the XOMDocument has NOT been initialized")
      Log(LastException.Message)
   End If
End Sub

But the XML will not be parsed, the XOMBuilder get un-initialized. Any idea, what could be wrong?
 
Upvote 0
Top