Making an HTTP Connection to a Web Server Using
Java[tm] 2 Platform, Micro Edition CLDC and MIDP
by Allen Lai
(April 2002)
We want to hear from you! Please send us your
FEEDBACK.
SECTION A - Short Description/Synopsis
The following code example may contain actual
software programs in source code form. This source code is made available for
developers to use as needed, pursuant to the terms and conditions of this
license.
With the Java[tm] 2 Platform, Micro Edition (J2ME[tm]) Connected Limited Device
Configuration (CLDC) 1.0.3 and MIDP (Mobile Information Devices Profile) 1.0.3,
making an HTTP connection to a webserver involves using the Generic Connection framework
and the javax.microedition.io.* package of MIDP. All CLDC/MIDP
implementations must implement the HTTP connection protocol according to the specifications. What this
means is that for an HTTP connection in a CLDC/MIDP application, one would use the HttpConnection
interface as defined in the javax.microedition.io.* package, which extends the interface javax.microedition.io.ContentConnection.
This document is intended to simply demonstrate the use of the HttpConnection
interface to make a connection to a URL.
Therefore, it does not contain additional code for user interface design and
hence, does not implement the CommandListener.
The code sample opens up an HTTP connection to the URL, obtains the
data through an InputStream, and
then
displays the raw HTML content onto the screen console.
NOTE: This code sample will compile and preverify successfully. However,
if the code is tested with a
CLDC/MIDP emulator environment, connecting and using the InputStream()method
to get a large amount of HTML
or a large file on a webserver may cause the emulator environment to run out
of application memory and will output
this error::
java.lang.OutOfMemoryError
at com.sun.midp.midlet.Scheduler.schedule(+328)
at com.sun.midp.dev.DevMIDletSuiteImpl.schedule(+7)
at com.sun.midp.Main.runLocalClass(+20)
at com.sun.midp.Main.main(+68)
SECTION B - Task and Skeleton code
I. Task List
- Import the relevant MIDP classes (a) javax.microedition.midlet
and (b) javax.microedition.io.
- Initialise a global string object with the desired URL to connect
to. Starting with the startApp() function that
the Java application manager calls:
- Initialise a String buffer object.
- Create an HttpConnection based on the URL String created
in task (2).
- Create an InputStream object
from the HttpConnection established above.
- Read in the byte stream data until the end of the file when a -1
is returned by the InputStream
method,while appending to the data to the String
buffer object.
- Close the InputStream.
- Close the HttpConnection connection.
- Convert the data in the buffer using the toString() method
and print out onto the system console.
- Run the pauseApp() method.
- Exit the application with the destroyApp() method.
Check the emulator message output screen or system console. The raw
HTML content of the URL should be displayed by the System.out.println() method.
II. Skeleton Code
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.IOException;
import java.io.InputStream;
public class MakeHttpConnection extends MIDlet {
private String URL = "http://www.foobar.com/index.html";
public void startApp() {
// parameter initialization
try {
//
open the HTTP connection to a URL
//
open an Input Stream to prepare to read in the byte data from the URL
//
appends the byte stream data into the buffer
//
close the Input Stream
//
close the HTTP connection
}
catch (( IOException e )
{
}
}
public void pauseApp() {
// any processing required
during when the MIDlet is in the Paused state
}
public void destroyApp( boolean unconditional ) {
// any cleaning up required
before the MIDlet exits
}
}
SECTION C - Source Code
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.IOException;
import java.io.InputStream;
public class MakeHttpConnection extends MIDlet {
private String URL = "http://www.foobar.com/index.html";
public void startApp() {
//creates a new String
buffer object and setting the HTTP connection object to null
StringBuffer buffer
= new StringBuffer();
HttpConnection c = null;
try {
// Display message that code is in startApp()
System.out.println("In startApp .....\n\n");
// Open an HTTP connection to the URL. The connection supports
// the HTTP 1.1 protocol defined in the IETF RFC2616.
c = (HttpConnection)Connector.open(URL);
// Open the InputStream using the HTTP connection that
// was established.
//
// Read the data until the end of the file where a -1 is returned.
InputStream is = c.openInputStream();
// Append the byte data into the String buffer.
int ch;
while ((ch = is.read()) != -1) { buffer.append( (char)ch );}
// Close the InputStream.
is.close();
// Close the HTTP connection
c.close();
// Prints out the data read from the URL
System.out.println( "String read from the URL is: \n\n" + buffer.toString()
);
pauseApp();
}
catch ( IOException e
) {
}
}
public void pauseApp() {
System.out.println("In pauseApp
.....\n\n");
destroyApp( true );
notifyDestroyed();
}
public void destroyApp( boolean unconditional
) {
System.out.println("In
destroyApp .....\n\n");
}
}
|