Hack 94. Create HTTP Connections

Create simple HTTP requests from a BlackBerry application.

The most powerful feature of programming BlackBerry applications is having the ability to make your applications perform wireless network connections to servers. The most common network connection type is HTTP allowing applications to connect to web servers anywhere on the Internet.

9.2.1. Create HTTP GET Requests

After importing the java.io.* and javax.microedition.io.* packages, here's is all you need in J2ME to create an HTTP connection:

 HttpConnection c = (HttpConnection)Connector.open("http://www.yahoo.com"); 

This will perform an HTTP connection to the Yahoo! web server. To read the response from the server, the first thing you must do is read the HTTP response code:

 int responseCode = c.getResponseCode(); // check response code 

The following step is to read the body of the HTTP response. To do this, you must open the InputStream of the response and read the stream until no more data is available:

 InputStream in = c.openInputStream(); int i = in.read(); StringBuffer sb = new StringBuffer(); while (i!=-1) { sb.append((char)i); i = in.read(); } 

One special requirement to perform network connections on the BlackBerry is that connections must be performed in a separate thread from the UI thread. For example, if the user clicks a menu item or presses a button, you cannot place your HTTP request code in the same thread as the event handler, you must spawn off a new thread. There are a couple of reasons for this: first, it prevents the BlackBerry UI from locking up while the request is in process; second, by default the BlackBerry will pop up a dialog box requesting that the user accept this network connection, as shown in Figure 9-1.

The user must select "Allow this connection" for the connection to succeed. If the user does not select to allow it, a security exception will be thrown to your application.

Here is some sample code that will spawn the HTTP connection to be performed in a separate thread:

 (new Thread(new Runnable( ) { public void run( ) { HttpConnection c = (HttpConnection)Connector.open("http://www.google.com"); c.getResponseCode( ); } })).start( ); 

9.2.2. Configure Connections to Use the MDS or Direct TCP/IP

One of the most unique and powerful features of the BlackBerry device is its ability to use a BlackBerry Enterprise Server (BES) to connect securely to an email server. Starting with BES 2.2 and above, there is a new feature called the Mobile Data Service (MDS), which allows applications to perform network connections through the BES server. You can think of the MDS as a VPN server to which the BlackBerry is permanently connected. All the data sent between the BlackBerry device and the MDS is encrypted exactly the same way as emails are encrypted by the BES. This means that your application can make connections to servers inside a private network without any ports needing to be opened on the firewall, since all connections are tunneled through the MDS server located inside the firewall.

Figure 9-1. Allow HTTP connection dialog

Another method of performing network connections from the BlackBerry device is by using the built-in TCP/IP stack. The TCP/IP stack allows the BlackBerry to make network connections using the carrier's network connection to the Internet, whether the device is attached to a BES server or not. To make TCP/IP connections, the device must have Handheld System Software 4.0 or above installed.

By default, most BlackBerry devices attempt to create their network connections using the MDS. There is one exception: iDEN BlackBerry devices make TCP/IP connections by default.

It is possible to make a special modification to your code to force a connection to use either the MDS or the TCP/IP stack:

 // this code will force the use of MDS Connector.openConnection("http://www.google.com;deviceside=false"); // this code will force to use the built in TCP/IP stack Connector.openConnection("http://www.google.com;deviceside=true"); 

Paul Dumais

BlackBerry Hacks
Blackberry Hacks: Tips & Tools for Your Mobile Office
ISBN: 0596101155
EAN: 2147483647
Year: 2006
Pages: 164
Authors: Dave Mabe

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net