BlackBerry 上5.0新提供的Network API:自动选择网关的终极解决方案

写了好几篇网络连接的博客,但其实在5.0上,这一切都已成为往事。BlackBerry 5.0发布中包含了不少有用的API,其中Network API当属一大利好。
其中的ConnectionFactory可以帮开发人员自动选择多种连接方式。也可以设置重试次数,自动支持HTTPS和SSL/TLS。总之用过了就体会到方便之处了。

新增加的Network API所有接口都列在下面,好东西和大家一起分享:

Package net.rim.device.api.io.transport 

This package contains the implementation of the Network API.

See:
          Description

Interface Summary
ConnectionAttemptListenerThis interface prescribes methods for a class that listens for connection attempts performed by a ConnectionFactory object.
 

Class Summary
ConnectionDescriptorThis class stores information about a Connection opened by using a ConnectionFactory.
ConnectionFactoryThis class enables you to create HTTP, HTTPS, socket, TLS, and SSL connections over supported transports.
TransportDescriptorThis class encapsulates information related to a specific transport instance.
TransportInfoThis class provides methods that provide information about the transport types available on a BlackBerry device.
 

Exception Summary
InsufficientCoverageExceptionThis class is used to indicate that the coverage for the chosen transport type is not adequate to create a reliable connection.
NoAvailableTransportExceptionThis class is used to indicate that no transport type is available to create a connection.
 

Package net.rim.device.api.io.transport Description

This package contains the implementation of the Network API.
The Network API attempts to simplify the establishment of connections over the different transports available on the Blackberry device.
It provides the means for querying the availability of transports and for selecting the most appropriated one to establish a connection.
It abstracts the details of parsing ServiceRecords and constructing proper URLs for Connector.open().
The Network API is intended for use with http://, https://, socket://, tls://, and ssl:// connections.

Determining transport availability and coverage

In order to successfully establish a connection, the desired transport has to be both available (i.e. the device must be capable of supporting that transport), and there must be sufficient coverage for the transport in question.

Availability of a specific transport type can be obtained as follows:

if ( TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_MDS) ) {
   // MDS transport is available
}
All the available transports can be obtained like this:
TransportDescriptor[] transports = getAvailableTransports();
All the available transport types can be obtained like this:
int[] transportTypes = getAvailableTransportTypes();
Determining available coverage for a specific transport type is achieved like this:
if ( TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_MDS) ) {
   // Connecting via MDS can be attempted
}

Establishing Connections

Obtaining a http connection using the first available transport:

// Create ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();

// use the factory to get a connection
ConnectionDescriptor conDescriptor = factory.getConnection("http://www.blackberry.com");

if ( conDescriptor != null ) {

   // connection succeeded
   int transportUsed = conDescriptor.getTransportDescriptor().getTransportType();

   // using the connection
   HttpConnection  httpCon = (HttpConnection) conDescriptor.getConnection();
   ...
}
Obtaining a http connection from a preference ordered list of transport types:
// make a list of transport types ordered according to preference (they will be tried in succession)
int[] preferredTransportTypes = {TransportInfo.TRANSPORT_MDS, TransportInfo.TRANSPORT_WAP2};

// Create ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();

// Configure the factory
factory.setPreferredTransportTypes( preferredTransportTypes );

// use the factory to get a connection
ConnectionDescriptor conDescriptor = factory.getConnection("http://www.blackberry.com");

if ( conDescriptor != null ) {

   // connection suceeded
   int transportUsed = conDescriptor.getTransportDescriptor().getTransportType();

   // using the connection
   HttpConnection  httpCon = (HttpConnection) conDescriptor.getConnection();
   ...
}
Obtaining a http connection over a specific transport type:
// Create ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();

// use the factory to get a connection
ConnectionDescriptor conDescriptor = factory.getConnection("http://www.blackberry.com", TransportInfo.TRANSPORT_WAP2, null);

if ( conDescriptor != null ) {

   // connection over WAP2 succeeded
   // using the connection
   HttpConnection  httpCon = (HttpConnection) conDescriptor.getConnection();
   ...
}
Obtaining a http connection over a specific transport:
// Create ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();

// use the factory to get a connection
ConnectionDescriptor conDescriptor = factory.getConnection("http://www.blackberry.com", TransportInfo.TRANSPORT_MDS, "S109234");


if ( conDescriptor != null ) {

   // connection to specific BES succeeded
   // using the connection
   HttpConnection  httpCon = (HttpConnection) conDescriptor.getConnection();
   ...
}
Proper connection tear-down:
SocketConnection conn = null; 
InputStream is  = null; 
OutputStream os = null; 

// Create ConnectionFactory
ConnectionFactory factory = new ConnectionFactory();

// use the factory to get a connection descriptor
ConnectionDescriptor conDescriptor = factory.getConnection("socket://www.blackberry.com:80");

if ( conDescriptor != null ) {

   // cast to proper type
  conn = (SocketConnection) conDescriptor.getConnection();
  
  // use the connection
  try {
      is = conn.openInputStream(); 
      os = conn.openOutputStream(); 
      . . .
  } catch (Throwable t) {   // can also catch specific exceptions to include special hadling for the different types
      // manage the exception
  } finally {
      // close streams in the opposite order from which they were opened, then close the connection
      try { os.close(); }   catch (Throwable t) {}
      try { is.close(); }   catch (Throwable t) {}
      try { conn.close(); } catch (Throwable t) {}
  }
}
 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值