通用连接框架:接口、实现与应用示例
立即解锁
发布时间: 2025-08-17 01:00:40 阅读量: 2 订阅数: 4 

### 通用连接框架:接口、实现与应用示例
在网络编程中,连接的管理和使用是至关重要的。通用连接框架提供了一系列接口,用于抽象和管理不同类型的连接。下面我们将详细介绍这些接口及其实现,并通过一个简单的程序示例来展示如何使用它们。
#### 连接接口层次结构
连接接口层次结构的顶层是`Connection`接口,它代表最通用、最抽象的连接类型。所有其他类型的连接都从它派生而来。`Connection`接口仅包含一个方法:
```java
public void close()
```
由于连接在`Connector`类创建时已经打开,因此该接口中没有`open()`方法。使用完连接后,应用程序必须关闭它。
#### 输入连接接口
`InputConnection`接口是`Connection`的直接子接口,它将连接的流数据表示为`InputStream`,即面向字节的数据流。该接口有两个方法,如下表所示:
| InputConnection 方法名 | 描述 |
| --- | --- |
| `DataInputStream openDataInputStream()` | 打开并返回一个连接到与该连接关联的网络资源的`DataInputStream` |
| `InputStream openInputStream()` | 打开并返回一个连接到与该连接关联的网络资源的`InputStream` |
#### 输出连接接口
`OutputConnection`接口也是`Connection`的子接口,它处理输出流,并将其流内容表征为面向字节的数据。其方法如下表所示:
| OutputConnection 方法名 | 描述 |
| --- | --- |
| `DataOutputStream openDataOutputStream()` | 打开并返回一个连接到与该连接关联的网络资源的`DataOutputStream` |
| `OutputStream openOutputStream()` | 打开并返回一个连接到与该连接关联的网络资源的`OutputStream` |
#### 流连接接口
`StreamConnection`接口直接扩展了`InputConnection`和`OutputConnection`接口,继承了它们的方法。它将连接抽象为最广义的数据流,即字节序列。该接口是一个空接口,不添加任何行为,但它在层次结构中起到了重要作用,代表任何可以将数据视为字节流的连接类型。
要建立到通信端口的连接,需要将 URI 传递给`Connector.open()`方法。地址的完整形式为:
```plaintext
address := <scheme>:<unit>;<parameters>
scheme := "comm"
unit := <integer representing comm port to open>
parameters := <device-specific configuration parameters>
```
例如,可以使用以下语句打开一个到通信端口的连接:
```java
StreamConnection conn = Connector.open("comm:0;baudrate=9600");
```
#### 内容连接接口
`ContentConnection`接口扩展了`StreamConnection`接口,它细化了流连接的概念,表征包含内容的连接。该接口定义了一些基本属性,适用于一类应用层协议。其方法如下表所示:
| ContentConnection 方法名 | 描述 |
| --- | --- |
| `String getEncoding()` | 返回表示消息内容所用字符编码集的字段值 |
| `long getLength()` | 返回消息的长度 |
| `String getType()` | 返回内容的类型 |
#### HTTP 连接接口
`HttpConnection`接口是`ContentConnection`的子接口,代表使用 HTTP 协议的连接。它支持发送请求、接收响应,并能够提取和解析 HTTP 字段。该接口还提供了获取连接信息的功能,其方法和常量定义如下表所示:
| HttpConnection 方法名 | 描述 |
| --- | --- |
| `long getDate()` | 返回日期头字段的值 |
| `long getExpiration()` | 返回过期头字段的值 |
| `String getFile()` | 返回此连接 URL 的文件字段的值 |
| `String getHeaderField(int n)` | 返回索引的键值头字段的值部分 |
| `String getHeaderField(String name)` | 返回具有指定键名的头字段的值 |
| `long getHeaderFieldDate(String name, long def)` | 返回具有指定键的头字段的值(解析为日期) |
| `int getHeaderFieldInt(String name, int def)` | 返回命名头字段的值(解析为整数) |
| `String getHeaderFieldKey(int n)` | 返回索引头字段的键部分 |
| `String getHost()` | 返回此连接 URL 的主机部分 |
| `long getLastModified()` | 返回 URL 的最后修改字段的值 |
| `int getPort()` | 返回此连接 URL 的端口字段的值 |
| `String getProtocol()` | 返回 URL 的协议名称 |
| `String getQuery()` | 返回 URL 的查询部分,即 URL 中第一个“?”之后的部分 |
| `String getRef()` | 返回 URL 的引用部分 |
| `String getRequestMethod()` | 返回当前请求方法 |
| `String getRequestProperty(String key)` | 返回命名的通用请求属性的值 |
| `int getResponseCode()` | 返回 HTTP 响应状态码 |
| `String getResponseMessage()` | 返回与响应状态码关联的 HTTP 响应消息 |
| `String getURL()` | 返回 URL 的字符串形式 |
| `void setRequestMethod(String method)` | 设置 URL 的方法;有效值为 GET、POST 和 HEAD |
| `void setRequestProperty(String key, String value)` | 设置指定通用请求属性的值 |
| HttpConnection 常量 | 描述 |
| --- | --- |
| `static String GET` | 表示 GET 请求方法 |
| `static String HEAD` | 表示 HEAD 请求方法 |
| `static int HTTP_ACCEPTED` | HTTP 状态 202 |
| `static int HTTP_BAD_GATEWAY` | HTTP 状态 502 |
| `static int HTTP_BAD_METHOD` | HTTP 状态 405 |
| `static int HTTP_BAD_REQUEST` | HTTP 状态 400 |
| ... | ... |
#### 示例程序
下面是一个简单的程序示例,展示了如何使用上述接口来请求 HTTP 资源的元信息。该程序由四个类组成:
- `ConnectionDemo`:定义了 MIDlet,显示一个表单,提示用户输入 URI。
- `URIEntry`:定义了一个表单,提示用户输入 URI,并在用户输入后实例化`ResourceDisplay`类。
- `ResourceDisplay`:定义了一个表单,显示所请求资源的元信息。
- `HttpResource`:处理连接建立、请求发送、响应接收和解析的实际工作。
```java
// Listing 8.1
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
/**
* This class defines the MIDlet for a demo that prompts
* the user for a URI, then makes an HTTP connection to an
* origin server and fetches a resource. The program uses
* a Form object to enable the user to enter the URI.
*/
public class ConnectionDemo extends MIDlet {
private static ConnectionDemo instance;
private URIEntry urlForm;
public ConnectionDemo() {
super();
instance = this;
}
/**
* Returns the single instance of this class. Calling
* this method before constructing an object will return
* a null pointer.
*
* @return an instance of this class.
*/
public static ConnectionDemo getInstance() {
return instance;
}
public void startApp() {
Display display;
URIEntry urlForm = URIEntry.getInstance();
display = Display.getDisplay(this);
display.setCurrent(urlForm);
}
public void pauseApp() {
}
void quit() {
destroyApp(true);
notifyDestroyed();
}
public void destroyApp(boolean destroy) {
instance = null;
}
/**
* Sets this object to be the current displayable object
* for the MIDlet.
*/
public void display() {
Display.getDisplay(this).setCurrent(urlForm);
}
}
// Listing 8.2
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
/**
* This class defines the Form that prompts the user for a
* URI to which the HTTP connection will be made. The user
* enters the URI and hits the "Go" command button.
*
* This class's instance then instantiates the helper
* ResourceDisplay class, which does the job of fetching
* the HTTP resource and displaying it.
*/
public class URIEntry extends Form implements CommandListener {
private static Command go = new Command("Go", Command.SCREEN, 1);
private static Command exit = new Command("Exit", Command.EXIT, 1);
private static URIEntry instance;
private TextField uri;
private Thread thread;
/**
* Constructor.
* @param title the title of the Form.
*/
private URIEntry(String title) {
super(title);
instance = this;
uri = new TextField("Connect to:", null, 70, TextField.URL);
uri.setString("http://");
append(uri);
addCommand(go);
addCommand(exit);
setCommandListener(this);
}
/**
* Returns the single instance of this class.
*
* @return an instance of this class.
*/
public static URIEntry getInstance() {
if (instance == null) {
instance = new URIEntry("Enter URL");
}
return instance;
}
/**
* Sets this object to be the current displayable object
* for the MIDlet.
*/
public void display() {
MIDlet m = ConnectionDemo.getInstance();
Display.getDisplay(m).setCurrent(this);
}
public void commandAction(Command c, Displayable d) {
if (c == go) {
ResourceDisplay view = new ResourceDisplay(uri.ge
```
0
0
复制全文
相关推荐










