June 24, 2009

Networking Helper Class

Filed under: Uncategorized — marwatk @ 9:20 pm

Enough users asked for help with networking after reading PodTrapper’s story that I decided to put something together to help with it.

This is the result. (This is the content of the class level javadoc, see the source for more information).

HttpConnectionFactory.java

This class aims to simply HTTP requests on the BlackBerry platform. Use of this class will require signing your application using RIM supplied signing keys.

The BlackBerry platform provides a multitude of different transports for network access. These include WiFi, BES, BIS, WAP2 and Direct TCP.

Not all transports are available on all devices, carriers or service plans. Ordinarily an application must determine on its own which transports are available for a given device, and attempt to connect via them in order. Sample use:

 HttpConnectionFactory factory = new HttpConnectionFactory(
    "http://www.versatilemonkey.com/test.txt",
    HttpConnectionFactory.TRANSPORT_WIFI | HttpConnectionFactory.TRANSPORT_BES );
 while( true ) {
    try {
       HttpConnection connection = factory.getNextConnection();
       try {
          connection.setRequestMethod( "POST" );
          connection.setRequestProperty(
                 "Content-type",
                 "application/x-www-form-urlencoded" );
          OutputStream os = connection.openOutputStream( );
          os.write( "foo=bar&var2=val2".getBytes() );
          os.close();
          InputStream is = connection.openInputStream();
          //do something with the input stream
          if( whatever we did worked ) {
             break;
          }
       }
       catch( IOException ) {
          //Log the error or store it for displaying to
          //the end user if no transports succeed
       }
    }
    catch( NoMoreTransportsException e ) {
       //There are no more transports to attempt
       Dialog.alert( "Unable to perform request" ); //Note you should never
                              //attempt network activity on the event thread
       break;
    }
 }
 

It is possible that an HttpConnection returned by getNextConnection will fail in a method undetectable before attemping the request.

Notes about the various transports from my experience:

Wifi:

Wifi is the least cost to the user and is also the fasted by orders of magnitude

BES/BIS:

- These are largely the same, except BES goes through the user’s corporate network and may be subject to corporate firewalls

- BES/BIS are generally offered as unlimited use to anyone with a BlackBerry specific data plan

- There is usually a limit imposed on the size of files that can be retrieved (usually 5mb, but can be as low as 100kb)

Direct TCP and WAP2:

- These transports use carrier data plans which are sometimes billed at the same rate as BES/BIS, but sometimes are billed separately. It is possible for users to be on unlimited data plans via BES/BIS but be charged per MB for TCP and WAP2. I have never seen the reverse, however.

- Some carriers do not have limits on the file size, others will timeout if you request a file over their limit (instead of a 413 error or similar)

- Some carriers have intermediary proxies that will alter the content of returned files (wrapping them in carrier specific content, for example)

Good luck, I hope this makes networking on BlackBerry easier for you.

Download the source

16 Comments

  1. This is great marcus, keep up the good work.. Cheers..

    Comment by Mantaker — September 7, 2009 @ 6:04 am

  2. I wish I’ve read this post an year earlier :) It would save me a lot of time.
    Anyway, here is a good explanation of different BB connections type, you may find it usefull.

    http://www.blackberry.com/DevMediaLibrary/view.do?name=NetworkingTransports

    Comment by QuiteSimple — September 9, 2009 @ 6:18 am

  3. [...] Versatilemonkey has a good Helper class that helps to encapsulate the extra parameters needed for the different connections. It is helpful code. There is also an interesting article from versatilemonkey on his BlackBerry development story. Worth reading. [...]

    Pingback by Just2us » Blog Archive » Developer Guide to BlackBerry Complex Connections — September 11, 2009 @ 11:10 pm

  4. [...] http://www.versatilemonkey.com/blog/index.php/2009/06/24/networking-helper-class/ Share This [...]

    Pingback by Blackberry Networking Helper Class — September 16, 2009 @ 9:01 am

  5. This is exactly what I was looking for. Good Job!

    Comment by Ron — September 23, 2009 @ 8:26 am

  6. Thank you so much for the code! It’s exactly what I’m looking for – it will save me tons of headache! Keep up the great work! Cheers!

    Comment by Tim Duncan — October 27, 2009 @ 1:57 pm

  7. Thanks a lot for such useful information and ready source code.
    I would like to know how to achieve BIS-Socket connection and not Direct-Tcp.
    Thanks in advance.

    Comment by Tejaswini — November 6, 2009 @ 12:14 am

  8. Hi there,Can you please explain what do this means?
    Im getting exception on getNextConnection,how do I resolve it…?

    “It is possible that an HttpConnection returned by getNextConnection will fail in a method undetectable before attemping the request”

    Thanks.

    Comment by Tejaswini — November 10, 2009 @ 12:36 am

  9. Will this class work for https connections? I’m new to RIM development in particular and Java development in general.
    If it won’t, do you mind if I copy and paste this code and try to modify it to create an HttpsConnectionFactory class?

    Comment by MLO — November 11, 2009 @ 12:30 pm

  10. [...] in use. Watch this video for a good overview. There are several code snippets out there but I found this one to be pretty good in terms of covering all the available options. Possibly related posts: [...]

    Pingback by Networking « BlackBerry development stories by James Harnedy — November 23, 2009 @ 11:58 pm

  11. Very good work!!
    The same idea can be applied to invoke non default browser.
    cheers!

    Comment by Klerisson — December 1, 2009 @ 1:36 am

  12. Thanks. Massive time (and migraine) saver.

    Comment by Rodney — December 2, 2009 @ 11:55 pm

  13. thanks very much,

    Comment by hakim — December 19, 2009 @ 11:10 am

  14. Thanks for the code…..but want to know is it also supports the https connentions..???

    Comment by amsiddh — February 17, 2010 @ 2:14 am

  15. Hi, thanks for posting this. We (as many) have been struggling with BB networking and decided to try out your code with people that could not get connected with ours and it worked for about 50% of those people out of about 20. But once we released it to a test group that used our previous version, many of them could not connect using your networking code as opposed to using our networking code. Do you know of any users that can not connect using the code you provided? Do you use this code in your production versions :) ? It seems that something is still missing to get it right for everyone (well, let’s say for 99% of users).

    I’d appreciate your help with this. I hope we can collaborate on this maybe to figure out the remaining kinks. If you are interested in sharing some knowledge and insights, please write me, I am sure this can be productive for everyone.

    Comment by Max — February 19, 2010 @ 2:34 pm

  16. Versatilemonkey…

    [...] something about versatilemonkey[...]…

    Trackback by Versatilemonkey — October 27, 2010 @ 2:46 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress