Wednesday, October 8, 2008

HTTP GET/POST from J2ME Application

For network support, J2SE defines over 100 interfaces, classes, and exceptions between the java.io and java.net packages. J2ME defines a subset of this functionality and provides a consolidated package for networking and file access in 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.

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. 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.

Code snippet for sending HTTP GET request to a URL is given below.

private String SendHttpGet( String url )
{
    HttpConnection hcon = null;
    DataInputStream dis = null;
    StringBuffer responseMessage = new StringBuffer();

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

        // Obtain a DataInputStream from the HttpConnection
        dis = new DataInputStream( hcon.openInputStream() );

        // Retrieve response from server
        int ch;
        while ( ( ch = dis.read() ) != -1 )
        {
            responseMessage.append( (char) ch );
        }
    }        
    finally
    {
        try
        {
            if ( hcon != null )
                hcon.close();
            if ( dis != null )
                dis.close();
        } catch ( IOException ioe )
        {
            ioe.printStackTrace();
        }
    }

    return responseMessage.toString();
}

Sample code for sending HTTP POST request to a URL is given below.

private String SendHttpPost( String url, String requestString )
{
    HttpConnection hcon = null;
    DataInputStream dis = null;
    DataOutputStream dos = null;
    StringBuffer responseMessage = new StringBuffer();

    try
    {
        // Open HttpConnection with both read and write access
        hcon = ( HttpConnection )Connector.open( url, Connector.READ_WRITE );

        // Set the request method to POST
        hcon.setRequestMethod( HttpConnection.POST );

        // Obtain DataOutputStream for sending the request string
        dos = hcon.openDataOutputStream();
        byte[] request_body = requestString.getBytes();

        // Send request string to server
        for( int i = 0; i < request_body.length; i++ )
        {
            dos.writeByte( request_body[i] );
        }

        // Obtain DataInputStream for receiving server response
        dis = new DataInputStream( hcon.openInputStream() );

        // Retrieve the response from server
        int ch;
        while( ( ch = dis.read() ) != -1 )
        {
            responseMessage.append( (char)ch );
        }
    }
    finally
    {
        try
        {
            if ( hcon != null )
                hcon.close();
            if ( dis != null )
                dis.close();
            if ( dos != null )
                dos.close();
        }
        catch ( IOException ioe )
        {
            ioe.printStackTrace();
        }
    }

    return responseMessage.toString();
}

No comments: