Android Question HttpServer OutputStream

XbNnX_507

Active Member
Licensed User
Hi, i'm using this library https://www.b4x.com/android/forum/t...er-in-your-android-application.25984/#content.

Whenever i try to write bytes to the outpustream with this code:
B4X:
        Dim r As Reflector
        r.Target = Response
        Dim jo As JavaObject = r.GetField("res")
        Dim ser As B4XSerializator
            
        Dim out As OutputStream = jo.RunMethod("getOutputStream", Null)
        Dim data() As Byte = ser.ConvertObjectToBytes( Object )
        out.WriteBytes( data, 0, data.Length )
i get java.io.EOFException: Unexpected end of ZLIB input stream at client side.
B4X:
Sub JobDone ( j as httjob )
  if j.success then
          dim data() as byte = bit.inpustreamtobytes( j.getinpustream) '< '-- Error
 end if
End Sub
Is there something wrong ?
Thanks.
 

XbNnX_507

Active Member
Licensed User
Are you closing the outputstream after you wrote to it?
Yes i did try that.
Can you post the full error message with the stack trace?
Here.
B4X:
Waiting for debugger to connect...
Program started.
java.io.EOFException: Unexpected end of ZLIB input stream
    at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:122)
    at java.io.DataInputStream.readByte(DataInputStream.java:265)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.readByte(B4XSerializator.java:133)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.readObject(B4XSerializator.java:301)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.ReadObject(B4XSerializator.java:112)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.ConvertBytesToObject(B4XSerializator.java:82)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator$2.call(B4XSerializator.java:93)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator$2.call(B4XSerializator.java:1)
    at anywheresoftware.b4a.BA$4.run(BA.java:272)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Error reading response: (EOFException) java.io.EOFException: Unexpected end of ZLIB input stream
 

OliverA

Expert
Licensed User
Log the size of you data array after serialization to make sure you’re sending anything
 

OliverA

Expert
Licensed User
Do you accidentally write anything else to the stream before/after?
 

OliverA

Expert
Licensed User
What type of Java object does gefield(“res”) return?
 

OliverA

Expert
Licensed User

XbNnX_507

Active Member
Licensed User
I don’t think that b4xserializator can do that. I think it’s only primitives, arrays, lists, types and maps. Arrays, list, types and maps can contain other arrays, list, types and maps.

Edit: it can also do strings (string is not a primitive). See slide on video at https://www.b4x.com/android/forum/threads/b4x-network-asyncstreams-b4xserializator.72149/#content
You misunderstood my question.
it has nothing to do with b4serializator. I'm converting a map ( CreateMap("test": "test") to bytes and then trying to send it over to the outpustream.
 

OliverA

Expert
Licensed User
You misunderstood my question.
Actually, I've overlooked the forum (B4A). I was thinking in terms of B4J. And yes, I also misunderstood what you are sending. So sort of ignore everything I've said so far.
 

OliverA

Expert
Licensed User
Back on track. Instead of
B4X:
dim data() as byte = bit.inpustreamtobytes( j.getinpustream) '< '-- Error
do a
B4X:
Log(j.GetString)
Just to see if all you are getting is some binary data.
 

XbNnX_507

Active Member
Licensed User
It does not return anything. Suppose is the normal behavior given that i'm not using Response.SendString().
 

OliverA

Expert
Licensed User
That should not matter (that you did not use sendstring)
 

OliverA

Expert
Licensed User
Try this:
B4X:
Log(Job.Response.ContentLength)
 

OliverA

Expert
Licensed User
Try this on the server side:
B4X:
out.WriteBytes( data, 0, data.Length )
out.Flush ' <- my guess is that it is not flushing the write. I'm guessing though
 

OliverA

Expert
Licensed User
The flush may be the wrong thing to do. A hint from this post (https://stackoverflow.com/a/5045073) indicates that ContentLength should be greater than 0 in order for the underlying servlet to recognize that it is done and flush the output stream. So you may need:
B4X:
'
        Dim r As Reflector
        r.Target = Response
        Dim jo As JavaObject = r.GetField("res")
        Dim ser As B4XSerializator

        Dim out As OutputStream = jo.RunMethod("getOutputStream", Null)
        Dim data() As Byte = ser.ConvertObjectToBytes( Object )
        jo.RunMethod("setContentLength", Array(data.Length)) '<--- This may do the trick
        out.WriteBytes( data, 0, data.Length )
 

XbNnX_507

Active Member
Licensed User
@OliverA
Well i have tried everything that you have posted with no success.
jo.RunMethod("setContentLength", Array(data.Length)) '<--- This may do the trick
Sadly it does not.

I even try to set the content type with Response.SetContentType("Application/octet-stream")
No success either.
 

OliverA

Expert
Licensed User
What did
B4X:
Log(Job.Response.ContentLength)
produce on the client side?
 
Top