Making a Connection with HTTP GET

Team-Fly

Loading data from a server is startlingly simple, particularly if you're performing an HTTP GET. Simply pass a URL to Connector's static open() method. The returned Connection will probably be an implementation of HttpConnection, but you can just treat it as an InputConnection. Then get the corresponding and read data to your heart's content.

In code, it looks something like this:

 String url = "http://jonathanknudsen.com/simple"; InputConnection ic = (InputConnection)Connector.open(url); InputStream in = ic.openInputStream(); // Read stuff from the InputStream ic.close(); 

Most of the methods involved can throw a java.io.IOException. I've omitted the try and catch blocks from the example for clarity.

That's all there is to it. You can now connect your MIDlets to the world.

Passing Parameters

With HTTP GET, all parameters are passed to the server in the body of the URL. This makes it easy to send parameters to the server. The following code fragment shows how two parameters can be passed:

 String url = "http://localhost/midp/simple?pOne=one+bit&pTwo=two"; InputConnection ic = (InputConnection)Connector.open(url); InputStream in = ic.openInputStream(); 

The first parameter is named "pOne" and has "one bit," as a value; the second parameter is named "pTwo" and has "two" as a value.

A Simple Example

HTTP isn't all about exchanging HTML pages. It's actually a generic file-exchange protocol. In this section, we'll look at an example that loads an image from the network and displays it. Listing 8-1 shows the source code for ImageLoader, a MIDlet that retrieves an image from the Internet and displays it on the screen.

Listing 8-1: This MIDlet retrieves an image from the Internet.

start example
 import java.io.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class ImageLoader     extends MIDlet {   private Item mItem;   public void startApp() {     final Display display = Display.getDisplay(this);     if (mItem == null) {       // Put up loading progress screen.       Form progressForm = new Form("Loading");       display.setCurrent(progressForm);       // Create the Form that will show the Image.       final Form imageForm = new Form("Image");       imageForm.addCommand(new Command("Exit", Command.EXIT, 0));       imageForm.setCommandListener(new CommandListener() {         public void commandAction(Command c, Displayable s) {           notifyDestroyed();         }       });       // Do network loading in a separate thread.       Thread t = new Thread() {         public void run() {           try {             String url = getAppProperty("Image-URL");             Image image = loadImage(url);             mItem = new ImageItem(null, image, 0, null);           }           catch (IOException ioe) {             mItem = new StringItem(null, ioe.toString());           }           imageForm.append(mItem);           display.setCurrent(imageForm);         }       };       t.start();     }   }   public Image loadImage(String url) throws IOException {     HttpConnection hc = (HttpConnection)Connector.open(url);     try {       int length = (int)hc.getLength();       byte[] data = new byte[length];       DataInputStream in = new DataInputStream(hc.openInputStream());       in.readFully(data);       return Image.createImage(data, 0, data.length);     }     finally {       hc.close();     }   }   public void pauseApp() {}   public void destroyApp(boolean unconditional) {} } 
end example

The loadImage() method contains all of the networking code. It's fairly simple; we pass the URL of an image (retrieved as an application property) to Connector's open() method and cast the result to HttpConnection. Then we retrieve the length of the image file, using the getLength() method. Given the length, we create a byte array and read data into it. Finally, having read the entire image file into a byte array, we can create an Image from the raw data.

You'll need to specify the MIDlet property "Image-URL" in order for this example to work correctly. Note that you need to specify the URL of a PNG image, not of a JPEG or GIF. The URL http://home.sprynet.com/~jknudsen/java2d_sm_ad.png produces the results shown in Figure 8-3.


Figure 8-3: The ImageLoader example

Note that this only works if the server sends back the content length, which we retrieve by calling the getLength() method of ContentConnection. The server is not required to send the content length; a more robust program would load the image file a different way if the content length were not supplied.


Team-Fly


Wireless Java. Developing with J2ME
ColdFusion MX Professional Projects
ISBN: 1590590775
EAN: 2147483647
Year: 2000
Pages: 129

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