Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Sample Code - Using the ConnectionFactory class in a BrowserField application

by Retired on ‎07-29-2010 12:07 PM - edited on ‎11-22-2010 07:35 PM by (15,603 Views)

This article applies to BlackBerry® Device Software 5.0 and higher.

 

Default connection settings are used when you create an instance of the BrowserField class.  These connection settings define the characteristics of how the BrowserField object makes HTTP requests for web content.  Examples include defining the order that network gateways are prioritized as well as specifying the various timeout and limit settings relating to network connections.

 

You can override these default connection settings by using your own ConnectionFactory class.

 

Using the ConnectionFactory class in your application involves the following four steps:

 

  1. Create an object that implements the ConnectionFactory class.  Specify your own connection settings, such as transport order and timeouts.
  2. Create a BrowserFieldConfig object
  3. Use the BrowserFieldConfig setProperty method to assign the ConnectionFactory object
  4. Create a BrowserField object, using the BrowserFieldConfig object in the constructor

 

Here is an example of using the ConnectionFactory class in your application (See Creating your first BrowserField2 application for more info):

 

MyConnectionFactory _factory = new MyConnectionFactory();

BrowserFieldConfig _config = new BrowserFieldConfig();
_config.setProperty(BrowserFieldConfig.CONNECTION_FACTORY, _factory);

BrowserField _bf2 = new BrowserField(_config);

MainScreen _screen = new MainScreen();
_screen.add(_bf2);
pushScreen(_screen);

_bf2.requestContent("http://devblog.blackberry.com");

 

The previous code sample used a fictitious class named MyConnectionFactory to implement the ConnectionFactory API.  Here is an example of how this class can be created:

 

 

package samplecode;

import net.rim.device.api.io.transport.ConnectionFactory;
import net.rim.device.api.io.transport.TransportInfo;

/**
* @description Demonstration of how to use ConnectionFactory class added to the 5.0 Device Software
* @version 1.0
* @author Adam Stanley (astanley@rim.com), Developer Relations
* @category BlackBerry BrowserField Development: ConnectionFactory API
* @since April 30, 2010
* @see http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/io/transport/ConnectionFactory.html
*/

/**
* This class enables you to create HTTP, HTTPS, socket, TLS, and SSL connections over supported transports.
* This class is not thread-safe and the getConnection() method blocks. So you should not call these methods from the main event thread of an application.
*/
public class MyConnectionFactory extends ConnectionFactory
{
public MyConnectionFactory()
{
//Sets the preferred transport types and priority/order used
// by the factory when attempting to get a connection:
setPreferredTransportTypes( new int[] {
TransportInfo.TRANSPORT_TCP_WIFI,
TransportInfo.TRANSPORT_MDS,
TransportInfo.TRANSPORT_TCP_CELLULAR,
TransportInfo.TRANSPORT_WAP2
});

//Sets the disallowed transport types used by the factory
// attempting to get a connection:
setDisallowedTransportTypes(new int[] {
TransportInfo.TRANSPORT_WAP,
TransportInfo.TRANSPORT_BIS_B
});


//Sets the maximum number of attempts the factory will make
// to create a connection. The default value is 1.
// Valid values range from 1 to 500:
setAttemptsLimit(10);

//Sets connectionTimeout to the desired value (ms):
setConnectionTimeout(10000);

//Sets the maximum time (ms) the factory will try to
// create a connection:
setTimeLimit(10000);
}
}

 

See the following learning resources for more information on the ConnectionFactory class:

 

Users Online
Currently online: 34 members 2,151 guests
Please welcome our newest community members: