站内搜索: 请输入搜索关键词
当前页面: 图书首页 > Wireless Java Developing with J2ME, Second Edition

Chapter 9: Connecting to the World - Wireless Java Developing with J2ME, Second Edition

Previous Section Next Section

Chapter 9: Connecting to the World

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 9-1 details the Connection interface and its various child interfaces. Plus signs indicate new interfaces in MIDP 2.0.

Click To expand
Figure 9-1: The Connection family tree

The link between the Connection interfaces and reality is a class called javax.microedition.io.Connector. 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 1.0 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 1.0-compliant devices may support additional types of connections, HTTP is the only one you should depend on. MIDP 2.0 adds mandatory support for HTTPS connections (secure HTTP) and standardizes the connection strings for several other types of connections. I'll discuss these new features later in this chapter.

HttpConnection's methods are detailed in Figure 9-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 9-2: The HttpConnection interface

Previous Section Next Section