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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 37
Registered: ‎10-20-2010
My Device: 9550
My Carrier: VZW
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. 

 

Developer
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.

Developer
Posts: 37
Registered: ‎10-20-2010
My Device: 9550
My Carrier: VZW

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?

Developer
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.

Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: HttpConnection Length Always -1

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

Highlighted
Developer
Posts: 37
Registered: ‎10-20-2010
My Device: 9550
My Carrier: VZW

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();
			}

 

 

Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: HttpConnection Length Always -1

short version Smiley Happy:

 

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();
}