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

Powered by WordPress