B4J Tutorial Consuming http streams

Standard http communication is made of a request being sent from the client to the server and the server response sent back to the client.
As I'm sure you know you should use OkHttpUtils2 for such requests.

There are other protocols such as WebSockets that allow bi-directional and long lasting communication between the client and server.

There are some cases where the server keeps the connection open and streams data to the client. Assuming that WebSockets are not used then such cases require non-standard handling.

The attached MJPEG class uses a custom OutputStream (implemented with inline Java) together with B4XBytesBuilder to read the data as it comes and keep the connection open.
It is a port of this decoder, which was implemented with raw sockets and AsyncStreams.
The Data_Available event is raised whenever new data is available.

Note that this code can be easily ported to B4A.

If making many connections at the same time then it is better to move OkHttpClient out of the class and reuse it.

java_7NSX9XANRZ.png
 

Attachments

  • MjpegWithOkHttp.zip
    4.5 KB · Views: 694
Last edited:

jcesar

Active Member
Licensed User
Longtime User
Hi

Can i use this code to stream audio from a online radio ?
 

woniol

Active Member
Licensed User
Longtime User
Hi,

Can we have an exmaple code ported to B4A.
With the original custom OutputStream does not work with B4A.
 

woniol

Active Member
Licensed User
Longtime User
Changed:
B4X:
Private const PackageName As String = "b4j.example" '<------ change as needed
to
B4X:
Private const PackageName As String = "b4a.example" '<------ change as needed
and it works like a charm :)
 

Hypnos

Active Member
Licensed User
Longtime User
Can this example run on raspberry pi ? I tried but no image display...
 

Hypnos

Active Member
Licensed User
Longtime User
The code itself should work. Test it on the PC first, it is possible that the mjpeg stream used in that example is no longer working.

I think it may related to the java/jfx version on raspberry pi, I followed below post to change to another version and I can see the webcam image but this time I got some other issue (e.g. button not work, screen background changed to black... etc..) : (

 

GMan

Well-Known Member
Licensed User
Longtime User
May be simple as this (don't know) ?
B4X:
Private const PackageName As String = "b4i.example" '<------ change as needed
 
Top