Network communication development

Anything that isn't a bug or feature request goes here

Network communication development

New postby Pavel on Tue Jun 23, 2009 12:22 am

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
Pavel
 

Re: Network communication development

New postby marwatk on Tue Jun 23, 2009 7:07 pm

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
Versatile Monkey
For more/quicker help make sure to check out the user manuals
marwatk
Lead (and only) Monkey
 
Posts: 2596
Joined: Sun Nov 02, 2008 9:20 pm

Re: Network communication development

New postby marwatk on Sat Jun 27, 2009 10:31 am

I put together that class if you're interested.

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

-Marcus
Versatile Monkey
For more/quicker help make sure to check out the user manuals
marwatk
Lead (and only) Monkey
 
Posts: 2596
Joined: Sun Nov 02, 2008 9:20 pm

Re: Network communication development

New postby Pavel on Sun Jun 28, 2009 9:57 am

Hey Marcus, good job!

Thanks a lot!
Pavel
 

Re: Network communication development

New postby sashishk on Thu Sep 17, 2009 2:04 am

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
sashishk
 
Posts: 3
Joined: Thu Sep 17, 2009 1:46 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 7 guests

cron