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
mrtrav
Posts: 37
Registered: ‎10-20-2010
My Device: 9550
Accepted Solution

HttpConnection Length Always -1

 

try 
{
	HttpConnection httpCon = (HttpConnection) Connector.open("http://www.blackberry.com");
    	httpCon.setRequestMethod(HttpConnection.GET);
    	long length = httpCon.getLength();
	int status = httpCon.getResponseCode();
	add(new RichTextField("Status: " + Integer.toString(status)));
	add(new RichTextField("Length: " + Long.toString(length)));
} 
catch (IOException e) 
{
	e.printStackTrace();
}

 

Above is my basic code to get an HttpConnection to an external website.  No matter what website I change to (as long as it's a valid website), I am getting a response code of 200 and a length of -1.  Doesn't matter if the MDS simulator is running or not, the results are always the same.  Am I missing a snippet of code to get the correct length?  

 

Also, when I use getEncoding(), this will return a null value. 

 

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: HttpConnection Length Always -1

I don't think length is guarenteed unless the server/page supports writing it in the response header.

Please use plain text.
Developer
mrtrav
Posts: 37
Registered: ‎10-20-2010
My Device: 9550

Re: HttpConnection Length Always -1

If the length is always -1, then how am I support to create the byte array to read the content?

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: HttpConnection Length Always -1

Pipe the InputStream to a ByteArrayOutputStream and then create the byte array after you've read everything from the InputStream.

Please use plain text.
Developer
YamilBracho
Posts: 713
Registered: ‎05-31-2010
My Device: 8520

Re: HttpConnection Length Always -1

Do something like ;
byte[] buffer = IOUtilities.streamToBytes(connection .openInputStream());

Please use plain text.
Developer
mrtrav
Posts: 37
Registered: ‎10-20-2010
My Device: 9550

Re: HttpConnection Length Always -1

If anyone stumbles across this in the future, this is the code I used to get it to work.  Thanks guys!

 

 

try {
    			HttpConnection httpCon = (HttpConnection) Connector.open("http://www.blackberry.com");
    			httpCon.setRequestMethod(HttpConnection.GET);
				
				 InputStream inputStream = httpCon.openInputStream();
				 ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
				 int z = 0;
		            while ((z = inputStream.read()) != -1) {
		                arrayOutputStream.write(z);
		         }
		         String msg = new String(arrayOutputStream.toByteArray());
		         add(new RichTextField(msg));
			} catch (IOException e) {
				e.printStackTrace();
			}

 

 

Please use plain text.
Developer
YamilBracho
Posts: 713
Registered: ‎05-31-2010
My Device: 8520

Re: HttpConnection Length Always -1

short version :smileyhappy::

 

try {
	HttpConnection httpCon = (HttpConnection) Connector.open("http://www.blackberry.com");
	httpCon.setRequestMethod(HttpConnection.GET);
	 String msg = new String(IOUtilities.streamToBytes(httpCon.openInputStream()));
	 add(new RichTextField(msg));
} catch (IOException e) {
	e.printStackTrace();
}

 

 

Please use plain text.