Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Sending HTTP Requests from a Wireless J2ME Device-2 : Page 2


Wireless Network Programming with J2ME
The networking capabilities of Java are quite robust. The Java 2 Standard Edition (J2SE) defines well over 100 interfaces, classes, and exceptions between the java.io and java.net packages. The functionality made available through these libraries is great for traditional computer systems with substantial CPU power, active memory, and persistent data storage, but not for most wireless devices. Consequently, the Java 2 Micro Edition (J2ME) defines a subset of this functionality and provides a consolidated package for networking and file access—the javax.microedition.io package. Due to the wide variety of mobile devices, this package merely defines a set of interfaces, leaving the actual implementation up to each vendor. This provides an optimum balance between portability and the utilization of device specific features.

The abstract networking and file I/O framework defined by the javax.microedition.io classes are referred to as the Generic Connection Framework (GCF). The GCF defines a set of related abstractions to represent different communication methods. The top-level abstraction is called Connection, from which six more interfaces are declared (four direct, and two indirect). These seven interfaces are declared as a part of J2ME's Connected Limited Device Configuration (CLDC), which is the configuration used by most Java-enabled wireless devices. This is designed to provide common networking and file I/O capabilities for all CLDC devices (cell phones, two-way pagers, low-end PDAs, etc.).

Although the purpose behind the GCF is to provide a common networking and file I/O framework, device manufacturers are not compelled to implement all of the interfaces declared in the GCF. The manufacturer may decide to only support socket connections (used with circuit-switched wireless networks), while others may choose to only support datagram-based communication (used with packet-switched networks). In order to promote portability across similar devices the Mobile Information Device Profile (MIDP) specification requires that all MIDP devices implement the HttpConnection interface. HttpConnection is not part of the GCF, but it does derive from one of the GCF interfaces, ContentConnection. We will use the HttpConnection interface to build our sample application.

Sending an HTTP GET Request
The code that will be explained in this section and the next section will only address the Generic Connection Framework interfaces and the HttpConnection interface used for sending an HTTP request and retrieving the response returned by the server. The code for creating the MIDP user interface can be downloaded at the end of the article.

We will begin by defining a method to house the code for sending an HTTP GET request. Since several of the operations contained in the method can potentially throw an IOException, we will throw such exceptions to the calling method.

   public String sendHttpGet( String url ) throws IOException {

        HttpConnection        hcon = null;

        DataInputStream       dis = null;

        StringBuffer          message = "";

        try {

The first step is to open a connection to the server using the Connector class, the crux of the GCF. We will then cast this connection to the specific type needed, in this case the HttpConnection type.

        hcon = ( HttpConnection ) Connector.open( url );

Next, we obtain a DataInputStream on our HttpConnection, allowing us to read the server's response data, character by character.

       dis = new DataInputStream( hcon.openInputStream() );

Using the read() method of DataInputStream, each character of the server's response is collected and appended to the StringBuffer object.

        int ch;

        while ( ( ch = dis.read() ) != -1 ) {

        message = message.append( ( char ) ch );


Finally (no pun intended), the connection objects are cleaned up to preserve resources, and the message is returned from the method.

        } finally {

        if ( hcon != null ) hcon.close();

        if ( dis != null ) dis.close();

        }//end try/finally

        return message.toString();

    }//end sendGetRequest( String )

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date