Chapter 8: Connecting to the World

Team-Fly

It's cool running Java on mobile phones and pagers, but the real kicker is getting your MIDlets connected to the Internet. With an Internet connection, you can write applications that allow you to access information and do work from your mobile telephone, from wherever you are in the world.

The Generic Connection Framework

The CLDC defines an extremely flexible API for network connections, the generic connection framework. It's all contained in the javax.microedition.io package and based around the Connection interface. Figure 8-1 details the Connection interface and its various child interfaces. I've omitted the methods of DatagramConnection and HttpConnection in this diagram; we'll explore them in detail later.

click to expand
Figure 8-1: The Connection family tree

The link between the Connection interfaces and reality is a class called javax.microedition.io.Connector. In fact, it's the only class in the javax.microedition.io package. The basic idea is that you pass a connection string to one of Connector's static methods and get back some Connection implementation. A connection string looks something like a URL, but there are various other possibilities. The connection string socket://apress.com:79 might open a TCP/IP connection to apress.com on port 79, then return a StreamConnection implementation.

MIDP simplifies this generic framework considerably by only requiring one type of connection, Hypertext Transfer Protocol (HTTP). You pass an HTTP URL to Connector and get back an implementation of HttpConnection. Although MIDP compliant devices may support additional types of connections, HTTP is the only one you should depend on.

HttpConnection's methods are detailed in Figure 8-2. Most of the methods in HttpConnection have to do with details of HTTP, which I won't cover here. I'll cover everything you need to know to connect to a server here, including both GET and POST requests. If you need to dig deeper, you can read RFC 2616 (one of the Internet standards documents), available at http://www.faqs.org/rfcs/rfc2616.html. Note that MIDP uses a subset of the full HTTP 1.1; only the GET, POST, and HEAD commands are required.

click to expand
Figure 8-2: The HttpConnection interface


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