www.pudn.com > j2mewireless_examples.zip > NetClientMIDlet.java
/*
* Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
*/
package examples.netclient;
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
/**
* A simple network client.
*
* This MIDlet shows a simple use of the HTTP
* networking interface. It opens a HTTP POST
* connection to a server, authenticates using
* HTTP Basic Authentication and writes a string
* to the connection. It then reads the
* connection and displays the results.
*/
public class NetClientMIDlet extends MIDlet
implements CommandListener {
private Display display; // handle to the display
private Form mainScr; // main screen
private Command cmdOK; // OK command
private Command cmdExit; // EXIT command
private Form dataScr; // for display of results
/**
* Constructor.
*
* Create main screen and commands. Main
* screen shows simple instructions.
*/
public NetClientMIDlet() {
display = Display.getDisplay(this);
mainScr = new Form("NetClient");
mainScr.append("Hit OK to make network connection");
mainScr.append(" ");
mainScr.append("Hit EXIT to quit");
cmdOK = new Command("OK", Command.OK, 1);
cmdExit = new Command("Exit", Command.EXIT, 1);
mainScr.addCommand(cmdOK);
mainScr.addCommand(cmdExit);
mainScr.setCommandListener(this);
}
/**
* Called by the system to start our MIDlet.
* Set main screen to be displayed.
*/
protected void startApp() {
display.setCurrent(mainScr);
}
/**
* Called by the system to pause our MIDlet.
* No actions required by our MIDLet.
*/
protected void pauseApp() {}
/**
* Called by the system to end our MIDlet.
* No actions required by our MIDLet.
*/
protected void destroyApp(boolean unconditional) {}
/**
* Gets data from server.
*
* Open a connection to the server, set a
* user and password to use, send data, then
* read the data from the server.
*/
private void genDataScr() {
ConnectionManager h = new ConnectionManager();
// Set message to send, user, password
h.setMsg("Esse quam videri");
h.setUser("book");
h.setPassword("bkpasswd");
byte[] data = h.Process();
// create data screen
dataScr = new Form("Data Screen");
dataScr.addCommand(cmdOK);
dataScr.addCommand(cmdExit);
dataScr.setCommandListener(this);
if (data == null || data.length == 0) {
// tell user no data was returned
dataScr.append("No Data Returned!");
} else {
// loop trough data and extract strings
// (delimited by '\n' characters
StringBuffer sb = new StringBuffer();
for (int i = 0; i < data.length; i++) {
if (data[i] == (byte)'\n') {
dataScr.append(sb.toString());
sb.setLength(0);
} else {
sb.append((char)data[i]);
}
}
}
display.setCurrent(dataScr);
}
/**
* This method implements a state machine that drives
* the MIDlet from one state (screen) to the next.
*/
public void commandAction(Command c,
Displayable d) {
if (c == cmdOK) {
if (d == mainScr) {
genDataScr();
} else {
display.setCurrent(mainScr);
}
} else if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
}
}
}