j2me 聯網技術分析總結
Generic Connections
In the CLDC Generic Connection framework, all connections are created using
the open
static method from the Connector
class. If
successful, this method returns an object that implements one of the generic
connection interfaces. Figure 1 shows how these interfaces form an is-a
hierarchy. The Connection
interface is the base interface such
that StreamConnectionNotifier
is a Connection
and
InputConnection
is a Connection
too.

Figure 1: Connection interface hierarchy
- The
Connection
interface is the most basic connection type. It can only be opened and closed. - The
InputConnection
interface represents a device from which data can be read. ItsopenInputStream
method returns an input stream for the connection. - The
OuputConnection
interface represents a device to which data can be written. ItsopenOutputStream
method returns an output stream for the connection. - The
StreamConnection
interface combines the input and output connections. - The
ContentConnection
is a subinterface ofStreamConnection
. It provides access to some of the basic meta data information provided by HTTP connections. - The
StreamConnectionNotified
waits for a connection to be established. It returns aStreamConnection
on which a communication link has ben established. - The
DatagramConnection
represents a datagram endpoint.
The open
method of the Connector
class has the
following syntax, where the String
parameter has the format
"protocol:address;parameters"
.
Here are a few examples:
HTTP Connection
Datagram Connection
Communicate with a Port
Open Files
The HttpConnection Interface:
The HTTP protocol is a request-response application protocol in which the parameters of the request must be set before the request is sent. The connection could be in one of the three following states:
- Setup: No connection yet
- Connected: Connection has been made, the request has been sent, and some response is expected
- Closed: Connection is closed
In the setup state the following methods can be invoked:
setRequestMethod
setRequestProperty
For example, suppose you have this connection:
Connector.open("http://java.sun.com/developer");
Then, you can set the request method to be of type POST
as follows:
And likewise, you can set some of the HTTP properties. For example, you
can set the User-Agent
as follows:
If there is a method that requires data to be sent or received from the server, there is a state transition from Setup to Connected. Examples of methods that cause the transition include:
openOutputStream
openDataInputStream
openDataOutputStream
getLength
getType
getDate
getExpiration
And while the connection is open, some of these methods that may be invoked:
getProtocol
getHost
getPort

------------------------------------------------------------
要注意的問題:
開發中遇到個很頭疼的問題, 與服務器通信write()數據時報java.io.IOException: Couldn't write to socket.
但是服務器抓不到任何包. 一開始懷疑是連建立連接出的問題, 實際上服務器抓不到包也有可能是流在沒有close的時候就已經報錯了.
如:
conn.open("url");
out = conn.openDataOutputStream();//此時將進行與服務器的三次握手;
//但是如果在out.close()之前出現異常服務器是抓不到任何包的
out.write(byte[] bb);
關于這個的解釋應該是流的緩沖機制.
所以正確的寫法應該是捕捉到異常之后在catch塊中把流close掉.
服務器端開發人員一般會說收不到包所以連接有問題,會把責任推給客戶端,抓住這個證據在跟服務器端的同事扯皮時將處于有利的位置,嘎嘎.
還有就是要多做小實驗, 注意代碼要規范嚴格.
發現的幾個問題:
1. java.io.IOException: Couldn't write to socket
2. java.io.IOException: Couldn't read from socket
CMNET聯網方案:
CMWAP聯網方案:
移動資費頁的處理:
一個通用的HTTP連接封裝:
posted on 2008-11-04 16:22 LukeW 閱讀(379) 評論(0) 編輯 收藏 所屬分類: J2ME 、協議 、遇到的問題及解決