Android Question HttpServer OutputStream

Discussion in 'Android Questions' started by XbNnX_507, Mar 17, 2019 at 6:59 AM.

Tags:
  1. XbNnX_507

    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:
    Code:
    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.
    Code:
    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.
     
  2. DonManfred

    DonManfred Expert Licensed User

    Are you closing the outputstream after you wrote to it?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    The OutputStream doesn't need to be closed. It is managed by the server.

    Can you post the full error message with the stack trace?
     
  4. XbNnX_507

    XbNnX_507 Active Member Licensed User

    Yes i did try that.
    Here.
    Code:
    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
     
  5. OliverA

    OliverA Well-Known Member Licensed User

    Log the size of you data array after serialization to make sure you’re sending anything
     
  6. XbNnX_507

    XbNnX_507 Active Member Licensed User

    Code:
    Dim data() As Byte = ser.ConvertObjectToBytes( object) 
     
    Log ( data.Length ) ' returns 132
     
  7. OliverA

    OliverA Well-Known Member Licensed User

    Do you accidentally write anything else to the stream before/after?
     
  8. OliverA

    OliverA Well-Known Member Licensed User

    What type of Java object does gefield(“res”) return?
     
  9. XbNnX_507

    XbNnX_507 Active Member Licensed User

    It's supposed to return javax.servlet ServletResponse interface ( java :confused:) . I'm just guessing here maybe @Erel could answer that.
     
  10. OliverA

    OliverA Well-Known Member Licensed User

  11. XbNnX_507

    XbNnX_507 Active Member Licensed User

    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.
     
  12. OliverA

    OliverA Well-Known Member Licensed User

    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.
     
    XbNnX_507 likes this.
  13. OliverA

    OliverA Well-Known Member Licensed User

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

    XbNnX_507 Active Member Licensed User

    It does not return anything. Suppose is the normal behavior given that i'm not using Response.SendString().
     
  15. OliverA

    OliverA Well-Known Member Licensed User

    That should not matter (that you did not use sendstring)
     
  16. OliverA

    OliverA Well-Known Member Licensed User

    Try this:
    Code:
    Log(Job.Response.ContentLength)
     
  17. OliverA

    OliverA Well-Known Member Licensed User

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

    OliverA Well-Known Member 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:
    Code:
    '
            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 )
     
  19. XbNnX_507

    XbNnX_507 Active Member Licensed User

    @OliverA
    Well i have tried everything that you have posted with no success.
    Sadly it does not.

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

    OliverA Well-Known Member Licensed User

    What did
    Code:
    Log(Job.Response.ContentLength)
    produce on the client side?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice