Look into it ~

          present
          隨筆 - 32, 文章 - 0, 評論 - 3, 引用 - 0
          數(shù)據(jù)加載中……

          j2me 聯(lián)網(wǎng)技術(shù)分析總結(jié)

          基本點(diǎn):

          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.

          fig1.gif
          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. Its openInputStream method returns an input stream for the connection.
          • The OuputConnection interface represents a device to which data can be written. Its openOutputStream method returns an output stream for the connection.
          • The StreamConnection interface combines the input and output connections.
          • The ContentConnection is a subinterface of StreamConnection. 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 a StreamConnection 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".

          Connector.open(String);

          Here are a few examples:

          HTTP Connection

          Connector.open("http://java.sun.com/developer");

          Datagram Connection

          Connector.open("datagram://address:port#");

          Communicate with a Port

          Connector.open("comm:0;baudrate=9600');

          Open Files

          Connector.open("file:/myFile.txt");


          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:

          HttpConnection c = (HttpConnection)
          Connector.open(
          "http://java.sun.com/developer");

          Then, you can set the request method to be of type POST as follows:

          c.setRequestMethod(HttpConnection.POST);

          And likewise, you can set some of the HTTP properties. For example, you can set the User-Agent as follows:

          c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");

          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:

          openInputStream
          openOutputStream
          openDataInputStream
          openDataOutputStream
          getLength
          getType
          getDate
          getExpiration

          And while the connection is open, some of these methods that may be invoked:

          getURL
          getProtocol
          getHost
          getPort


          ------------------------------------------------------------
          要注意的問題:
          開發(fā)中遇到個(gè)很頭疼的問題, 與服務(wù)器通信write()數(shù)據(jù)時(shí)報(bào)java.io.IOException: Couldn't write to socket.
          但是服務(wù)器抓不到任何包. 一開始懷疑是連建立連接出的問題, 實(shí)際上服務(wù)器抓不到包也有可能是流在沒有close的時(shí)候就已經(jīng)報(bào)錯了.
          如:
          conn.open("url");
          out = conn.openDataOutputStream();//此時(shí)將進(jìn)行與服務(wù)器的三次握手;
                                            //但是如果在out.close()之前出現(xiàn)異常服務(wù)器是抓不到任何包的
          out.write(byte[] bb);

          關(guān)于這個(gè)的解釋應(yīng)該是流的緩沖機(jī)制.
          所以正確的寫法應(yīng)該是捕捉到異常之后在catch塊中把流close掉.
          服務(wù)器端開發(fā)人員一般會說收不到包所以連接有問題,會把責(zé)任推給客戶端,抓住這個(gè)證據(jù)在跟服務(wù)器端的同事扯皮時(shí)將處于有利的位置,嘎嘎.
          還有就是要多做小實(shí)驗(yàn), 注意代碼要規(guī)范嚴(yán)格.

          發(fā)現(xiàn)的幾個(gè)問題:

          1. java.io.IOException: Couldn't write to socket

          2. java.io.IOException: Couldn't read from socket

          CMNET聯(lián)網(wǎng)方案:

          CMWAP聯(lián)網(wǎng)方案:

          移動資費(fèi)頁的處理:

          一個(gè)通用的HTTP連接封裝:

          posted on 2008-11-04 16:22 LukeW 閱讀(382) 評論(0)  編輯  收藏 所屬分類: J2ME協(xié)議遇到的問題及解決


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 大邑县| 清远市| 绥江县| 甘德县| 拉萨市| 延川县| 九台市| 宣威市| 观塘区| 土默特右旗| 门头沟区| 杭州市| 诸暨市| 文成县| 巴彦淖尔市| 彰武县| 浦东新区| 黎城县| 梨树县| 沧源| 波密县| 西青区| 武威市| 日照市| 天台县| 平利县| 广德县| 晋宁县| 兴化市| 通江县| 涿州市| 安远县| 奇台县| 阜平县| 泰和县| 嘉善县| 岳西县| 上栗县| 麻江县| 商南县| 疏勒县|