Page 1 of 1

Facing issue with HTTPConnectionFactory usage

New postPosted: Thu Sep 17, 2009 3:07 am
by sashishk
Hey Marcus,

The HTTPConnectionFactory class is wonderful implementation. I've included this into my code. I'm calling the connection factory as in below attached code:

String hitURL(String url)
{
HttpConnectionFactory factory = new HttpConnectionFactory(url, HttpConnectionFactory.TRANSPORTS_ANY);
String message = "";
while( true )
{
try
{
HttpConnection connection = factory.getNextConnection();
try
{
connection.setRequestMethod( "GET" );
InputStream input = connection.openInputStream();
.........get data from stream........
message = data fetched;
input.close();
break;
}
catch( IOException e)
{
try
{
connection.close();
}
catch(IOException e1){}
//Log the error or store it for displaying to the end user if no transports succeed
//Note you should never attempt network activity on the event thread
Dialog.alert( "Unable to perform request!!!" );
}
}
catch(NoMoreTransportsException e)
{
//There are no more transports to attempt
//Note you should never attempt network activity on the event thread
Dialog.alert( "No more transports available...."+e.toString());
break;
}
}
return message;
}

It works well but sometimes on device and simulator, when I try to make a call to internet, it gets hanged. Following are the differences w.r.t the code you have put on public domain for usage of the connection factory.
1) GET used instead on POST
2) In case of exception I have given dialog alert after closing the connection

Please suggest if the above changes can cause the app to be in hang state if I use post, or is Dialog is shown in case of exception.

How can I know if the signal strength has gone down during the course of fetching the data after the connection was established?

Thanks
-aks

Re: Facing issue with HTTPConnectionFactory usage

New postPosted: Thu Sep 17, 2009 8:28 pm
by marwatk
Hi there,

Two issues. The simulator hang a lot during network communication. It just sucks that way. I have to do proof of concepts in the simulator and do real network testing on device, it's annoying.

Second is that you should never call network operations and UI operations from the same thread. You'll need to do all UI operations in the event thread, but doing network operations there will get your app terminated by the OS.

I'd love to help further, but I'm not really set up for that. I'd definitely suggest the main BB support forums java development site. The guys over there are awesome.
http://supportforums.blackberry.com/rim ... d=java_dev

Hope it works out for you.

-Marcus

Re: Facing issue with HTTPConnectionFactory usage

New postPosted: Thu Sep 17, 2009 11:14 pm
by sashishk
Thanks Marcus.....

-aks

Re: Facing issue with HTTPConnectionFactory usage

New postPosted: Mon Sep 28, 2009 10:45 am
by RLord321
Hello Marcus. I'm still waiting for my new project to complete so I can revisit my old ones and include your factory. Just curious, though..Why wouldn't you always want to use TRANSPORTS_ANY when setting up the connection? I was thinking about this and the only reason I can think of is if you only want your app to run in High Speed connections like WiFi. Is this right? There shouldn't be any other downside in using TRANSPORTS_ANY other than this, right?

Thanks

Re: Facing issue with HTTPConnectionFactory usage

New postPosted: Mon Sep 28, 2009 7:05 pm
by marwatk
Well, with PodTrapper, for example, most users don't want to download episodes over the cell network, but only via wifi. I can imagine other apps might run in to similar issues so I made it an option in that code. Does that make sense?

-Marcus

Re: Facing issue with HTTPConnectionFactory usage

New postPosted: Tue Sep 29, 2009 7:34 am
by RLord321
Makes perfect sense. Again, thanks for posting the HTTPConnectionFactory as I'm sure it is saving thousands of hours of development across the Blackberry community.

Re: Facing issue with HTTPConnectionFactory usage

New postPosted: Thu Dec 16, 2010 3:43 pm
by Annon
THANKS to the Creator