Java Reference
In-Depth Information
File
file://addesses.dat
F ileConnection
Bluetooth
" bluetooth://psm=1001 "
S treamConnection
The only connection type that is guaranteed to be available in MIDP 1.0 is the HTTP protocol. PDAP
adds specialized interfaces for access to serial ports and file systems. Whether a certain protocol is
actually available depends on the device. For example, it does not make sense to support the comm
protocol on devices without a serial port.
Creating a Connection—The Connector Class
In the GCF, connections are established using the class Connector . By passing a URL describing the
protocol to the open method of the Connector class, a connection is established. For example, the
following line opens a Hypertext Transfer Protocol (HTTP) connection to the address
http://java.sun.com :
try {
Connection connection = Connector.open ("http://java.sun.com");
}
catch (IOException e) {
// an error occurred while opening the connection.
}
In the case of an error, an IOException is thrown. Most of the GCF methods can throw an
IOException in order to report I/O errors to the application.
If the connection is established successfully, an instance of a class implementing the Connection
interfaces is returned. Other special interfaces derived from Connection are available for different
connection types like datagram or stream connections. Figure 6.1 gives an overview of the
corresponding GCF interfaces.
Figure 6.1. The connection interfaces of the GCF.
The general Connection interface itself does not implement much functionality. It only defines the
close() method that is needed to terminate a connection that was opened using the open() method
of the Connector class. The functionality for reading and writing data is added in
InputConnection and OutputConnection . The InputConnection defines two methods
 
 
Search WWH ::




Custom Search