Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 71
Registered: ‎11-23-2009
My Device: Not Specified
Accepted Solution

How to retrieve data from a connection response when the connection length is unknown(-1)?

I am using the following code to retrieve response data from a connection:

 

long len = _httpConnection.getLength();
is = _httpConnection.openInputStream();
_data=new byte[(int)len];
is.read(_data);
returnContent=new String(_data);

But how can I form a byte array when the length is -1 though the response contains proper data.

Any ideas/ better way to fetch the inputstream?

 

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: How to retrieve data from a connection response when the connection length is unknown(-1)?

Something like this..

 

int ch;
while ((ch = is.read()) != -1) {
 ...
}

 

 


Use Search. "Accept Solution" If the problem is resolved.
Highlighted
Developer
Posts: 71
Registered: ‎11-23-2009
My Device: Not Specified

Re: How to retrieve data from a connection response when the connection length is unknown(-1)?

In this case can u tell me how can I save the 'ch' into a string.

As in String doesn't take this int parameter.

 

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: How to retrieve data from a connection response when the connection length is unknown(-1)?

StringBuffer sb = new StringBuffer();
while ((ch = is.read()) != -1) 
{
   sb.append((char)ch);
}

 


Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 71
Registered: ‎11-23-2009
My Device: Not Specified

Re: How to retrieve data from a connection response when the connection length is unknown(-1)?

Thanks,

That works fine.

Just a question. This way I can retrieve the data in every situation regardless of the content length. Just read the data till the end.

So its better. Isnt it? By the way why is it unable to determine the length in some cases  while the data is there and all perfect?

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: How to retrieve data from a connection response when the connection length is unknown(-1)?

You can use this method.. but this all depends on what you recieving and the lenght of the data etc..

 

Why teh -1 is coming, probably from serverside the content-lenght is not set for the connection?


Use Search. "Accept Solution" If the problem is resolved.