Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming data with HttpURLConnection

I want to use HttpURLConnection to open a long-living connection to a server via HTTP POST and process streaming data as it comes in. This is for an Android project.

It's unclear to me how to do this with HttpURLConnection. In iOS, NSURLConnection provides an asynchronous mode where a user-implemented callback gets called as new data comes in and is available for parsing.

With the HttpURLConnection in Java, I see there's an available() method that returns the number of bytes that can be read. But, its unclear how I would write a streaming app that opens a connection and reacts as new streaming data becomes available to parse off a DataInputStream.

I would appreciate some input. Does HttpURLConnection or an alternate class in Java support non-blocking, asynchronous processing of HTTP connections?

Thanks

like image 928
Paul Avatar asked Sep 16 '25 04:09

Paul


1 Answers

After doing more research, I've confirmed that Java doesn't have any built-in support for asynchronous HTTP. A packaged called Jetty contains a HTTP client with built-in asynchronous HTTP and callbacks. Its available at:

http://wiki.eclipse.org/Jetty/Tutorial/HttpClient

like image 132
Paul Avatar answered Sep 18 '25 19:09

Paul