国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

無線開發:如何使用kxml解析WAP

2019-11-18 12:46:23
字體:
來源:轉載
供稿:網友

  一、兩種訪問方法:
  
  目前的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的程序與無線設備之間的通訊和數據交換。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 济宁市| 寿光市| 陆川县| 锡林郭勒盟| 梁平县| 中方县| 花莲市| 新晃| 台前县| 西昌市| 囊谦县| 黄梅县| 营口市| 阳朔县| 邹平县| 馆陶县| 定兴县| 本溪市| 卢龙县| 垦利县| 丰顺县| 淄博市| 无极县| 疏附县| 邵阳县| 瑞安市| 梨树县| 阳原县| 巴东县| 元江| 右玉县| 镇康县| 长泰县| 千阳县| 永吉县| 饶阳县| 易门县| 年辖:市辖区| 珲春市| 建昌县| 渝北区|