Home > BlackBerrry > BlackBerry HTTP Connection Factory w/ inline support for multiple network type

BlackBerry HTTP Connection Factory w/ inline support for multiple network type

December 18th, 2009

This Api supports various network type and failover and coverage checks on BlackBerry device.

http://www.versatilemonkey.com/HttpConnectionFactory.java

priority order:

public static final int DEFAULT_TRANSPORT_ORDER[] = { TRANSPORT_WIFI, TRANSPORT_BES, TRANSPORT_BIS,
TRANSPORT_DIRECT_TCP, TRANSPORT_WAP2 }

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;
    }
 }
 
 

Comments are closed.