Look into it ~

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

          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.

          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


          ------------------------------------------------------------
          要注意的問題:
          開發中遇到個很頭疼的問題, 與服務器通信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協議遇到的問題及解決


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


          網站導航:
           
          主站蜘蛛池模板: 永年县| 石屏县| 广东省| 克拉玛依市| 砀山县| 太和县| 平远县| 社旗县| 宕昌县| 玛纳斯县| 亳州市| 胶南市| 彰武县| 海林市| 密云县| 玉屏| 兴仁县| 景泰县| 准格尔旗| 哈尔滨市| 福建省| 红桥区| 若尔盖县| 望谟县| 彭泽县| 工布江达县| 平罗县| 安阳市| 曲周县| 萍乡市| 纳雍县| 竹溪县| 绥芬河市| 庆城县| 沽源县| 柳林县| 安溪县| 长葛市| 郓城县| 筠连县| 富源县|