Network Programming with J2ME
This final article in a four-part series on Java 2 Micro Edition (J2ME) development addresses the networking capabilities of J2ME included within the javax.microedition.io package.
The previous three articles in this series demonstrated J2ME support for graphical user interface development and local device data storage via the Record Management System (RMS). One other very important feature is the ability to open network connections and transfer data using the J2ME Connection framework. This framework is contained within the javax.microedition.io package and includes the Connection class and several useful interfaces (including StreamConnection, ContentConnection, and HTTPConnection). This article discusses the design of this package and presents some enhancements to the AddressBookMIDLet example (from previous articles) to make use of the StreamConnection and ContentConnection interfaces.
The javax.microedition.io Package
If you have any experience developing with the J2SE java.net package, you know that it's very comprehensive and includes support for some very advanced networking capabilities. Unfortunately, these advanced features make it impossible to fit the java.net package into the necessary Connected Limited Device Configuration (CLDC) footprint. Instead, we're presented with a slimmed-down, yet completely functional connection framework that allows simple connections to be made for data transfer. The MID profile goes a step further and also defines an HTTPConnection interface for HTTP access over the network.
Modifications to AddressBookMIDLet
The examples in this article are nearly the same as those in the Record Management System AddressBook example from earlier articles. The differences revolve around the fact that, instead of hard-coding the addresses into the program, I'll retrieve the addresses from a text file stored on the Internet, using J2ME networking capabilities. This text file (named addressbook.txt) contains the comma-delimited names and addresses used in the AddressBookMIDLet example. As I mentioned earlier, the following examples use two different J2ME interfaces to perform this transfer: StreamConnection and ContentConnection.