Page 1 of 1

Network communication development

New postPosted: Tue Jun 23, 2009 12:22 am
by Pavel
Hi Marcus,

thanks for your story article. It gave me a lot of information.

These days I'm also starting some BB development and I found interesting the part of your article about Network communication. Since HTTP communication is going to be alpha and omega of my app (pure XML transfers), I would like to understand it more. Basically, do you think there is some problem in simply using code below for HTTP connection?:

StreamConnection s = (StreamConnection)Connector.open(myUrl);
HttpConnection httpConn = (HttpConnection)s;
int status = httpConn.getResponseCode();

I thought that BB does all the other work for you (is Wifi available? -> is BES available? -> is BIS available? -> is TCP available? ...) Am I wrong?

Thanks for for help,
Pavel

Re: Network communication development

New postPosted: Tue Jun 23, 2009 7:07 pm
by marwatk
Hi Pavel,

No, until you get to OS version 5.0 (so I hear) you have to do all the routing by hand.

Enough people are having trouble with this that I think I'm going to try to put together a simple HttpConnectionFactory and release it public domain. I'll probably post about it in the forums this weekend if I get a chance.

-Marcus

Re: Network communication development

New postPosted: Sat Jun 27, 2009 10:31 am
by marwatk
I put together that class if you're interested.

Grab it here:
http://www.versatilemonkey.com/blog/?p=3

-Marcus

Re: Network communication development

New postPosted: Sun Jun 28, 2009 9:57 am
by Pavel
Hey Marcus, good job!

Thanks a lot!

Re: Network communication development

New postPosted: Thu Sep 17, 2009 2:04 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