06-16-2009 05:47 PM
Hi, I would like to know if it is possible to get data from the input stream immediately or as they come in. The reason I ask is that we do a request to our web server and it will stream data down the connection for a period of time. The data isn't huge but is required real time.
I am finding it that if I request something, I am blocked at getResponseCode() until the stream ends say after 2mins. So I get nothing for the period of the wait. I tried not getting the response code and proceeded to doing open input stream netting the same result.
I am using 4.3 APIs, The following code is what I'm using to try the request.
_streamConnection = (HttpConnection)Connector.open(_url);
HttpConnection httpConn = (HttpConnection)_streamConnection;
// the following message will block until whole response is completed
int status = httpConn.getResponseCode();
if ( status == HttpConnection.HTTP_OK )
_inputStream = _streamConnection.openInputStream();
byte data = new byte;
int len = 0;
int size = 0;
StringBuffer raw = new StringBuffer();
while ( -1 != (len = _inputStream.read(data)) )
String received = new String(data, 0, len);
size += len;
_data = raw.toString();
I know the SocketConnection route works as we current have it working, but I was hoping that we could use this solution as it means we don't have to reinvent the wheel.
I tried looking through the APIs, search the forum and my conclusion
is no there isn't a way to read it immediately. But I'm posting as a
last glimmer of hope.
Thanks in advance for any help.
06-16-2009 05:57 PM
Is this the device or the simulator? If device, what OS level, and what carrier?
If simulator, are you running MDS?
How have you defined the URL? What connection parameters are you using?
06-16-2009 06:06 PM - edited 06-16-2009 06:10 PM
This is on the simulator with MDS Running. The simulator is 9000-OrangeFR-126.96.36.199.
I am not using any additional parameters in my url. As far as I am aware it should be using device=false; as a default which means going through BES MDS.
sample url is something like:
http://<ip address>/<virtual path>/DEH.ashx?client=<internal protocol>
06-16-2009 06:25 PM
I'm not sure if HttpConnection can be made to behave the way you want. One thing to check is whether the server is including a content-length header in the response. There's a (remote) chance that HttpConnection will behave better if such a header is sent; otherwise it will certainly try to read to the end to determine the length of data.
Another possible approach, assuming you control both sides here, is to break the transaction into multiple requests (using, say byte-range request headers) and use keep-alive connections. You might also want to experiment with multipart/byteranges responses. (But either of those may be worse than falling back on socket connections.)
06-16-2009 09:06 PM
Just a random suggestion, if you formatted the http data yourself and processed the http headers yourself on the response, then you could just open a socket connection to port 80?
06-16-2009 09:29 PM
Peter, yes you are correct in that. We do have a implementation of Socket Connection to read the input stream and it works. I just wanted to make sure that we don't have to reinvent the wheel with decoding the headers and interpretation of error codes just because we want to read stream immediately. Which is why I wanted to ensure that I cannot use HttpConnection the way I want it before going any further with the SocketConnection implementation path.
Ted, the server is using chunk encoding and it does not contain content-length header being send with the response. So I believe the behaviour you describe is true.
Details on Chunk encoding in http headers. http://en.wikipedia.org/wiki/Chunked_transfer_enco