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
<?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>
新闻热点
疑难解答