標籤:android wbxml kxml android
WAP Binary XML定義好XML片斷表述出同步伺服器位址、遠端資料庫名稱、登入帳號等等內容一、兩種存取方法: 目前的kxml支援兩種wap格式:WBXML/WML。 而有兩種方法將解析WBXML: 1。使用j2me將WBXML轉換到XML; 2。使用kxml直接解析WBXML流。下面我在這裡討論一下使用第二種方法實現client代碼解析WBXML,當然要使用kxml了。 二、kxml實現方法: 首先需要位於web server的應用程式通過開放WAP網關(關於JWAP:詳見http://jwap.sourceforge.net/)發送WML檔案給j2me client。在WAP網關將資料發送j2me client之前WAP網關將WML檔案轉換為了WBXML檔案。下面代碼的展示了j2me client如何接收WBXML資料,解析資料,並顯示有用的資料在手機螢幕上。 需要注意,在本常式中使用的kxml v1.0版本,kxml v2.0版本在使用上可能有所不同,開發人員可以參考kxml2的手冊。 import java.io.*; import org.kxml.*; import org.kxml.parser.*; import org.kxml.wap.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import javax.microedition.io.*; public class WbxmlTest extends MIDlet implements CommandListener { private Display display = null; private List menu = null; private Form form = null; private String incomingText = ""; static final Command okCommand = new Command("Ok", Command.OK, 1); static final Command exitCommand = new Command("Exit", Command.EXIT, 0); // This is a hard coded WSP message that contains // address of web server where our jsp page is located. byte[] message ={ (byte)‘1‘,(byte)0x40,(byte)0x3D,(byte)‘h‘,(byte)‘t‘, (byte)‘t‘,(byte)‘p‘,(byte)‘:‘,(byte)‘/‘,(byte)‘/‘, (byte)‘l‘,(byte)‘o‘,(byte)‘c‘,(byte)‘a‘,(byte)‘l‘, (byte)‘h‘,(byte)‘o‘,(byte)‘s‘,(byte)‘t‘,(byte)‘:‘, (byte)‘8‘,(byte)‘0‘,(byte)‘8‘,(byte)‘0‘,(byte)‘/‘, (byte)‘e‘,(byte)‘x‘,(byte)‘a‘,(byte)‘m‘,(byte)‘p‘, (byte)‘l‘,(byte)‘e‘,(byte)‘s‘,(byte)‘/‘,(byte)‘j‘, (byte)‘s‘,(byte)‘p‘,(byte)‘/‘,(byte)‘f‘,(byte)‘i‘, (byte)‘n‘,(byte)‘a‘,(byte)‘l‘,(byte)‘f‘,(byte)‘i‘, (byte)‘l‘,(byte)‘e‘,(byte)‘s‘,(byte)‘/‘,(byte)‘D‘, (byte)‘a‘,(byte)‘t‘,(byte)‘.‘,(byte)‘j‘,(byte)‘s‘, (byte)‘p‘,(byte)0x80,(byte)0x94,(byte)0x88,(byte)0x81, (byte)0x6A,(byte)0x04,(byte)0x83,(byte)0x99 }; // Memory space to receive message. byte[] msg = new byte [256]; public void pauseApp() { /* ----- */ } public void destroyApp(boolean unconditional) { notifyDestroyed(); } public void startApp() { display = Display.getDisplay(this); this.mainMenu(); }//startApp //Displays the menu screen private void mainMenu() { menu = new List(" Send Request", Choice.IMPLICIT); menu.append(" Send Message",null); menu.addCommand(okCommand); menu.setCommandListener(this); display.setCurrent(menu); }//mainMenu //Display the reply from WAPGateway (JWap). private void showReply() { form = new Form( "Incoming Message" ); form.append("The price = " + incomingText); form.addCommand(exitCommand); form.setCommandListener(this); display.setCurrent(form); }//showReply // Makes a WSP Connection with a WAPGateway, // Sends a message and receives the reply. public void getConnect() { Datagram dgram =null; DatagramConnection dc=null; try { dc = (DatagramConnection)Connector.open ("datagram://127.0.0.1:9200"); dgram = dc.newDatagram(message, message.length); try{ dc.send(dgram);} catch (InterruptedIOException e){ e.printStackTrace(); } dgram = dc.newDatagram (msg,msg.length); try{ dc.receive(dgram);} catch (InterruptedIOException e){ e.printStackTrace();} catch( IOException e){ e.printStackTrace();} // This is the most interesting part. incomingText = this.getIncomingTextOfWmlc(dgram.getData()); this.showReply(); dc.close(); }//try catch (IllegalArgumentException ie){ ie.printStackTrace(); } catch (ConnectionNotFoundException cnf){ cnf.printStackTrace(); } catch (IOException e){e.printStackTrace();} }//getConnect() private String getIncomingTextOfWmlc ( byte[] wmlc ) { try { // Remove WSP header. // We know it is 19 bytes for our case. // But for real world applications, // this should be dynamically deteced. for ( int j = 0; j < wmlc.length-19; j++ ) wmlc[j] = wmlc[j+19]; WmlParser parser = new WmlParser(new ByteArrayInputStream(wmlc)); while (true) { try { ParseEvent parseEvent = parser.read(); if ( parseEvent.getType() == Xml.START_TAG ) { Attribute attr = parseEvent.getAttribute("value"); if ( attr != null ) return attr.getValue(); }//if }//try catch ( IOException e) {} }//while }//try catch ( IOException e) { e.printStackTrace(); } return "error"; }//getIncomingTextOfWmlc public void commandAction(Command c, Displayable d) { String commandlabel = c.getLabel(); if (commandlabel.equals("Exit")) destroyApp(false); else if (commandlabel.equals("Ok")) getConnect(); }//commandAction }//class WbxmlTest 為了示範目的,除了建立一個web Server外,還要在本機建立一個JWAP Server。 三、代碼說明: 上面的代碼將資料連線請求發送到了原生JWAP Server的URL:“datagram://127.0.0.1:9200”,並發送了一個硬式編碼WSP(wireless Session Protocol)請求:http://localhost:8080/examples/jsp/finalfiles/Dat.jsp,然後等待並讀取JWAP Server的回應,在接收到回應資訊後使用kxml解析提取其中的資料(元素屬性名稱為“value”的屬性值)。在解析完成後,將資料顯示於手機螢幕上。 代碼中的getConnect 方法建立與JWAP Server的串連,並發送請求給JWAP Server,要求訪問web Server上的http://localhost:8080/examples/jsp/finalfiles/Dat.jsp,在接收到JWAP Server發回的請求後,getConnect方法調用getIncomingTextOfWmlc方法提取接收到的WBXML資料。由於j2me client與JWAP Server之間的通訊使用了WAP協議堆棧,所以j2me client接收的資料中包含WSP頭,在getIncomingTextOfWmlc方法中首先去掉了這個WSP頭。 之後,getIncomingTextOfWmlc方法使用KXML的事件解析機制進行了4步操作: 1。傳入儲存WBXML資料的位元組數組構造WmlParser 對象; 2。調用WmlParser的read方法,找到第一個TAG開始的地方; 3。讀取“value”屬性值; 4。回到第2步進行2、3之間的迴圈,直到找不到START_TAG。 四、資料流程: 而在JWAP網關接收到j2me client發來的寫入程式碼請求後,將這個請求轉寄給了web Server,本常式中的web Server為http://localhost:8080。web Server接收到請求後,使用一個硬式編碼WML檔案作為回應: <?xml version="1.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"> <%@ page language="java" contentType= "text/vnd.wap.wml" %> <wml> <card id="c0" newcontext="false" ordered="false"> <input type="Price" value="15224" emptyok="false"/> </card> </wml> 當JWAP網關接收到這個web Server的WML檔案後,將其轉換為WBXML格式並修改其content-type編碼為WBXML,最後將轉換後的WBXML格式資料發給了j2me client。 五、總結: 使用kxml方法避免了XML與WBXML之間的相互轉換,WBXML檔案的格式減少了XML檔案的大小,不僅可將WBXML用於WAP裝置,也可以用於基於web的程式與無線裝置之間的通訊和資料交換。