Android Question Need help getting bytes via http from old gopro camera

jimseng

Active Member
Licensed User
Longtime User
Hello.
I am trying to get status information from an old gopro camera via its inbuilt wifi AP. I request the status using job.Download("http://10.5.5.9/camera/sx?.password") and the response is supposed to be status bytes. I don't really know how I should be approaching this. In my JobDone event I log:

B4X:
Log(Job.Response.StatusCode)
    Log(Job.Response.ContentType)
    Log(Job.GetString)

And the output is like this:


The content length looks to be about what I would expect, although that could be a co-incidence.

I'm hoping this is enough information to help me understand how to receive the data correctly as getstring obviously isn't the right approach.
 

drgottjr

Expert
Licensed User
Longtime User
everything is bytes. but not all bytes are (UTF8,ASCII, etc) strings. how the bytes are interpreted depends on the content-type (if you're downloading from an http server).

application/octet-stream could be almost anything BUT a string. by using j.getString you have made the determination that the input is a UTF8 string. that's why you get nothing but the ?'s when you try to print out the result. the server has made it known that the data are not a string (application/octet-stream). you need j.getinputstream to capture the response as bytes. that's 1/2 the job. next you need to figure out what the bytes mean.

there is no sense in reading the same bytes over and over while you try to figure out what they mean, so let's capture the bytes to a file. then you can work with that.

B4X:
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.Response.GetHeaders)
       
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "gopro.dat",False)
        File.Copy2(j.GetInputStream, out)
        out.Close

        j.Release

        Log(File.Size(File.DirInternal,"gopro.dat") & " bytes saved")
       
        Dim gopro() As Byte = File.ReadBytes(File.DirInternal,"gopro.dat")
        Dim bc As ByteConverter
        Log( bc.HexFromBytes( gopro ))
    else
        log(j.errormessage)
    end if


you've already seen you can't "print" the bytes as UTF8 (because they're not), but you can display the bytes as a hex string. that - by itself - doesn't give you the answer, but it provides something to work with.

to understand what the bytes mean, you probably need help from gopro or from somebody who has already done what you're trying to do. you'll have to
do some research. application/octet-stream implies some kind of "application" is needed to handle the "octet-stream" (the bytes).

you might start here: https://gopro.github.io/OpenGoPro/http
seems to be an api for talking to a gopro device.
 
Last edited:
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
Thank you for that explanation. I actually have a list of what the bytes represent, although it is pretty easy to work out empirically. I was kind of half way there, I had forgotten about byteconverter, however one of the reasons I am struggling is I don't understand how to use job.getinputstream. Your explanation via a file works but how do I do it without saving to a file. Hopefully this snippet will explain my confusion.
B4X:
Dim out As OutputStream = File.OpenOutput(File.DirInternal, "gopro.dat",False)
        Dim inps As InputStream 'what should inps be if not inputstream
        inps = Job.GetInputStream '<----------------------------------why is this empty
        File.Copy2(Job.GetInputStream, out)
        out.Close
        Job.Release
        Log(File.Size(File.DirInternal,"gopro.dat") & " bytes saved")
        Dim gopro() As Byte = File.ReadBytes(File.DirInternal,"gopro.dat")
        Dim bc As ByteConverter
        Log( bc.HexFromBytes( gopro ))
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i prefer this way:
B4X:
        Dim out As OutputStream
        out.InitializeToBytesArray(0)
        
        Dim inp As InputStream = j.GetInputStream
        
        Dim buffer(1024) As Byte
        Dim count As Int
        count = inp.ReadBytes(buffer, 0, buffer.length)
        Log("this read: " & count & " bytes")
        
        Do While count > -1
            out.WriteBytes( buffer, 0, count )
            count = inp.ReadBytes(buffer, 0, buffer.length)
            Log("this read: " & count & " bytes")
        Loop
        
        
        Dim gopro() As Byte = out.ToBytesArray

        out.Close

        j.Release
     Log(gopro.Length & " raw bytes")
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…