通用連接框架(Generic Connection Framework,GCF)是在J2ME平臺(tái)中廣泛使用的用于聯(lián)網(wǎng)和IO處理的類。本文主要講述GCF的關(guān)鍵特性,然后把各種連接的關(guān)鍵信息總結(jié)為列表的形式,供開發(fā)者在使用GCF的時(shí)候參考。
使用GCF建立連接的方式類似下面程序片斷:
...
import java.lang.String;
import javax.microedition.io.*;
static Connection Connector.open( String name );
...
我們關(guān)注的主要是Connection和name參數(shù),下面解釋什么是name?什么是Connection。Name實(shí)際上是與Connection關(guān)聯(lián)的URI。形式如:
scheme : [address] [params]
其中scheme是協(xié)議的名稱,比如http;address是指定協(xié)議的連接端點(diǎn);params是一些可選的參數(shù),形式類似于language=Chinese。我們給出一個(gè)例子:http://www.j2medev.com/servlet?hello=world。
在介紹Connection之前,我們有必要說明一下MIDP規(guī)范中說明的一個(gè)問題,那就是設(shè)備支持什么協(xié)議。在MIDP規(guī)范中,協(xié)議支持的分類共三種:必須,推薦和可選。http和https是MIDP規(guī)范要求設(shè)備必須支持的,其他的協(xié)議都列入了推薦和可選類別中。在其他的可選包中通常也會(huì)用到GCF的擴(kuò)展,比如WMA使用了MessageConnection,JSR75使用了FileConnection。我們可以通過查詢系統(tǒng)屬性的方式來檢查設(shè)備是否支持相關(guān)的協(xié)議連接。比如,我們可能使用下面的方法查詢?cè)O(shè)備對(duì)jsr75的支持情況:
...
import java.lang.System;
import java.lang.String;
String name = "microedition.io.file.FileConnection.version";
String value = System.getPRoperty( name );
Boolean hasFile;
if ( ( value != null ) && value.equals( "1.0" ) )
hasFile = true;
else
hasFile = false;
...
在使用GCF的時(shí)候,我們首先應(yīng)該根據(jù)項(xiàng)目需求選擇適當(dāng)?shù)腃onnection。關(guān)于Connection的層次結(jié)構(gòu)可以從下圖清晰的了解到,
 
下面的部分總結(jié)了GCF支持的連接類型,這些信息按照接口、BNF語法和例子來分組,供開發(fā)者參考:
MIDP2.0(JSR 118)
串口連接
支持: 可選
接口: javax.microedition.io.CommConnection,擴(kuò)展了StreamConnection
BNF:
url ::== "comm:" port_id *(option_list)
port_id ::== 1*(characters)
option_list ::== *(baudrate bitsperchar stopbits parity blocking
autocts autorts)
baudrate ::== ";baudrate=" digits
bitsperchar ::== ";bitsperchar=" bit_value
bit_value ::== "7" "8"
stopbits ::== ";stopbits=" stop_value
stop_value ::== "1" "2"
parity ::== ";parity=" parity_value
parity_value ::== "even" "odd" "none"
blocking ::== ";blocking=" on_off
autocts ::== ";autocts=" on_off
autorts ::== ";autorts=" on_off
on_off ::== "on" "off"
例子:
"comm:0;bitsperchar=8;stopbits=1;parity=none"
"IR1"
可以使用下面的方法列出port_id的值:
import java.lang.System;
import java.lang.String;
String ports = System.getProperty( "microedition.commports" );
--------------------------------------------------------------------------------
HTTP連接
支持: 必須
接口: javax.microedition.io.HttpConnection ,擴(kuò)展了ContentConnection
定義: IETF RFC 1738, Section 3.3 HTTP
例子:
"http://java.sun.com"
"http://www.sun.com:80"
--------------------------------------------------------------------------------
HTTPS Connection
支持: 必須
接口: javax.microedition.io.HttpsConnection,擴(kuò)展了HttpConnection
例子:
"https://java.sun.com"
"https://www.sun.com:443"
--------------------------------------------------------------------------------
安全連接
支持: 推薦
接口: javax.microedition.io.SecureConnection ,擴(kuò)展了SocketConnection
BNF:
url ::== "ssl://" hostport
hostport ::== host ":" port
host ::== host name or ip address
port ::== numeric port number
例子:
"ssl://www.sun.com"
"ssl://java.sun.com:443"
注意: 只支持客戶端模式,設(shè)備上不支持服務(wù)器模式
--------------------------------------------------------------------------------
Server Socket Connection
支持: 推薦
接口: javax.microedition.io.ServerSocketConnection ,擴(kuò)展了StreamConnectionNotifier
BNF:
url ::== "socket://" "socket://" hostport
hostport ::== host ":" port
host ::== omitted for inbound connections
port ::== numeric port number (omitted for system assigned port)
例子:
"socket://:25"
"socket://:17"
"socket://"
--------------------------------------------------------------------------------
Socket Connection
支持: 推薦
接口: javax.microedition.io.SocketConnection ,擴(kuò)展了StreamConnection
BNF:
url ::== "socket://" hostport
hostport ::== host ":" port
host ::== host name or IP address (omitted for inbound connections)
port ::== numeric port number
例子:
"socket://developer.sun.com/techtopics/mobility:80"
"socket://www.sun.com:53"
--------------------------------------------------------------------------------
Datagram Connection
支持: 推薦
接口: javax.microedition.io.UDPDatagram.Connection ,擴(kuò)展了DatagramConnection
BNF:
url ::== "datagram://" "datagram://" hostport
hostport ::== host ":" port
host ::== host name or IP address (omitted for inbound connections)
port ::== numeric port number (omitted for system assigned port)
例子:
"datagram://" - server with system assigned port
"datagram://:7" - server with specified port
"datagram://java.sun.com:80" - client
JSR 75 PDA Optional Packages
查詢方式: System.getProperty( "microedition.io.file.FileConnection.version" ).equals( "1.0" );
接口: javax.microedition.io.file.FileConnection ,擴(kuò)展了StreamConnection
定義: IETF RFC 1738, Section 3.10 Files
例子:
"file:///CFCard/music/thewayup/opening.mp3"
"file:///RWM/photos/emma.jpg"
"file://localhost/RWM/photos/thekids.jpg"
JSR 82 Java APIs for Bluetooth Wireless Technology, version 
            
是否支持藍(lán)牙只能通過嘗試的方式得到了。
接口: javax.microedition.io.StreamConnection
接口: javax.microedition.io.StreamConnectionNotifier
BNF:
url ::== srvString cliString
srvString ::== "BTspp" ":" "http://" srvHost 0*5(srvParams)
cliString ::== "btspp" ":" "http://" cliHost 0*3(cliParams)
cliHost ::== btAddress ":" channel
srvHost ::== "localhost" ":" uuid
channel ::== %d1-30
uuid ::== 1*32(HEXDIG)
bool ::== "true" "false"
btAddress ::== 12*12(HEXDIG)
text ::== 1*( ALPHA DIGIT " " "-" "_" )
HEXDIG ::== "A".."F" "a".."f" "0".."9"
ALPHA ::== "A".."Z" "a".."z"
DIGIT ::== "0".."9"
name ::== ";name=" text
master ::== ";master=" bool
encrypt ::== ";encrypt=" bool
authorize ::== ";authorize=" bool
authenticate ::== ";authenticate=" bool
cliParams ::== master encrypt authenticate
srvParams ::== name master encrypt authorize authenticate
例子:
    "btspp://
    "btspp://localhost:8CDF20D
--------------------------------------------------------------------------------
Logical Link Control and Adaptation Profile (L2CAP)
接口: javax.bluetooth.L2CAPConnection ,擴(kuò)展了Connection
接口: javax.bluetooth.L2CAPConnectionNotifier ,擴(kuò)展了Connection
BNF:
url ::== srvString cliString
srvString ::== "btl2cap" ":" "http://" srvHost 0*7(srvParams)
cliString ::== "btl2cap" ":" "http://" cliHost 0*5(cliParams)
cliHost ::== address ":' psm
srvHost ::== "localhost" ":" uuid
psm ::== 4*4(HEXDIG)
receiveMTU ::== ";receiveMTU=" 1*(DIGIT)
transmitMTU ::== ";transmitMTU=" 1*(DIGIT)
cliParams ::== master encrypt authenticate receiveMTU transmitMTU
srvParams ::== name master encrypt authorize authenticate
receiveMTU transmitMTU
例子:
    "btl2cap://
    "btl2cap://localhost:8CDF20D
--------------------------------------------------------------------------------
Object Exchange Protocol (OBEX)
接口: javax.obex.sessionNotifier ,擴(kuò)展了Connection
接口: javax.obex.ClientSession ,擴(kuò)展了Connection
接口: javax.obex.Operation ,擴(kuò)展了ContentConnection
BNF:
url ::== tcpObex irdaObex btObex
btObex ::== btSrvString btCliString
tcpObex ::== tcpSrvString tcpCliString
irdaObex ::== irdaSrvString irdaCliString
tcpCliString ::== "tcpobex" ":" "http://" tcpHost
tcpSrvString ::== "tcpobex" ":" "http://" 0*1(ipPort)
ipPort ::== 1*(DIGIT)
ipAddress ::== 3*3(%d0-255 ".") (%d0-255)
ipName ::== 1*( hostLabel "." ) topLabel
topLabel ::== ALPHA ALPHA *(alphaNum "-") alphaNum
hostLabel ::== alphaNum alphaNum *(alphaNum "-") alphaNum
tcpHost ::== ipName 0*1(":" ipPort) ipAddress 0*1(colon ipPort)
btSrvString ::== "btgoep" ":" "http://" btSrvHost 0*5(btSrvParams)
btCliString ::== "btgoep" ":" "http://" btCliHost 0*3(btCliParams)
btCliParams ::== master encrypt authenticate
btSrvParams ::== name master encrypt authorize authenticate
btCliHost ::== btAddress ":" channel
btSrvHost ::== "localhost" ":" uuid
irdaSrvString ::== "irdaobex" ":" "http://" irdaSrvHost 0*1(irdaParams)
irdaCliString ::== "irdaobex" ":" "http://" irdaCliHost 0*1(irdaParams)
irdaSrvHost ::== "localhost" 0*1("." 1*(DIGIT))
irdaCliHost ::== "discover" 0*1("." 1*(DIGIT))
"addr." 2*8(HEXDIG)
"conn"
"name." 1*(characters)
irdaParams ::== ";ias=" 1*(characters) 0*("," 1*(characters))
characters ::== %d0-255
alphaNum ::== ALPHA DIGIT
例子:
"tcpobex://litespeed:6512"
"tcpobex://:6512"
    "btgoep://
    "btgoep://localhost:8CDF20D
"irdaobex://discover.08;ias=fax"
"irdaobex://localhost.08;ias=fax,OBEX,OBEX:IrXfer"
JSR 120 Wireless Messaging API (WMA), version 1.1
            
查詢方式: System.getProperty( "wireless.messaging.sms.smsc" ) != null
接口: javax.wireless.messaging.MessageConnection ,擴(kuò)展了Connection
BNF:
url ::== "sms://" address_part
address_part ::== foreign_host_address local_host_address
local_host_address ::== port_number_part
port_number_part ::== ":" digits
foreign_host_address ::== msisdn msisdn port_number_part
msisdn ::== "+" digits digits
digits ::== DIGIT DIGIT digits
例子:
"sms://+19055551212"
"sms://+14165551212:5009"
"sms://:5009"
--------------------------------------------------------------------------------
GSM Cell Broadcast Adapter
接口: javax.wireless.messaging.MessageConnection ,擴(kuò)展了Connection
BNF:
url ::== "cbs://" address_part
address_part ::== message_identifier_part
message_identifier_part ::== ":" digits
digits ::== DIGIT DIGIT digits
例子:
"cbs://:5009"
"cbs://:1089"
JSR 205 Wireless Messaging API (WMA), version 2.0
查詢方式: System.getProperty( "wireless.messaging.mms.mmsc" ) != null
接口: javax.wireless.messaging.MessageConnection ,擴(kuò)展了Connection
BNF:
url ::== "mms://" address_part
address_part ::== (e-mail-address device-address shortcode-address
application-id)
device-address ::== general-phone-address [application-id]
general-phone-address ::== (global-telephone-type "/TYPE=PLMN")
(ipv4 "/TYPE=IPv4")
(ipv6 "/TYPE=IPv6")
(escaped value "/TYPE=" address-type)
global-telephone-type ::== "+" *(DIGIT)
ipv6 ::== IETF RFC 1884
ipv4 ::== 1 *DIGIT "." 1*DIGIT "." 1*DIGIT "." 1*DIGIT
application-id ::== ":" [("com" "org" "edu" "gov")] [organization] [*(package-name)] class-name
organization ::== "." *applicationID-symbol
package-name ::== "." *applicationID-symbol
class-name ::== "." *applicationID-symbol
address-type ::== *address-char
address-char ::== *(ALPHA DIGIT "_")
e-mail-address ::== mailbox group
group ::== phrase "." [#mailbox] ";"
phrase ::== *(space Word space)
mailbox ::== addr-spec [phrase] route-addr
route-addr ::== "<" [route] addr-spec ">"
route ::== 1#("@" domain) ":"
addr-spec ::== local-part "@" domain
local-part ::== word *("." word)
domain ::== sub-domain *("." sub-domain)
sub-domain ::== domain-ref domain-literal
domain-ref ::== atom
domain-literal ::== "[" *(dtext quoted-pair) "]"
atom ::== ALPHA DIGIT "!" "#" "$" "%" "'" "*"
"+" "-" "=" "?" "{" "}" "" "~"
"^" "_"
word ::== atom quoted-string
quoted-string ::== '"' (qtext qpair) *endq"
qtext ::== (^'"')["/"]
endq ::== [^"http://"]
space ::== *(" ")
qpair ::== "http://."
shortcode-address ::== shortcode-string
shortcode-string ::== *(ALPHA DIGIT "!" '"' "$" "%" "&"
"/" "(" ")" "+" "*" "." "-"
"/" "=" "< ">" "[" "]" "_"
"^" "?" "{" "}" "'" "~" ";"
applicationID-symbol ::== ALPHA DIGIT "." "_"
例子:
"mms://+14165552112"
"mms://richard@sun.com"
"mms://+16475551212:com.sun.wireless.photo"
"mms://:com.sun.wireless.voicenote"
原文請(qǐng)參考這里
(出處:http://m.survivalescaperooms.com)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注