/* * HelloMidlet.java * * Created on November 18, 2007, 8:37 PM */ package hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; /** * * @author Fiona */ public class HelloMidlet extends MIDlet implements CommandListener { /** Creates a new instance of HelloMidlet */ public HelloMidlet() { } private Form helloForm; private StringItem helloStringItem; private Command exitCommand; private Command okCommand1; private TextField textField1; static final String url = "http://192.168.0.58/seng462/login.php"; Ticker response; /** This method initializes UI of the application. */ private void initialize() { // Insert pre-init code here getDisplay().setCurrent(get_helloForm()); // Insert post-init code here } /** Called by the system to indicate that a command has been invoked on a particular displayable. * @param command the Command that ws invoked * @param displayable the Displayable on which the command was invoked */ public void commandAction(Command command, Displayable displayable) { // Insert global pre-action code here if (displayable == helloForm) { if (command == exitCommand) { // Insert pre-action code here exitMIDlet(); // Insert post-action code here } else if (command == okCommand1) { // Insert pre-action code here invokeServlet(url); // Do nothing // Insert post-action code here } } // Insert global post-action code here } public void invokeServlet(String url) { HttpConnection con = null; InputStream is = null; // response or error message StringBuffer sb = new StringBuffer(); try { //con = (HttpConnection)Connector.open(url+"?username="+english.getString()); con = (HttpConnection)Connector.open(url+"?username="+textField1.getString()); con.setRequestMethod(HttpConnection.POST); con.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1"); // response is = con.openDataInputStream(); int ch; while ((ch = is.read()) != -1) { sb.append((char)ch); } } catch(IOException e) { sb = new StringBuffer("Error: Web Server Down"); } catch (Exception e) { sb = new StringBuffer("Error: " + e.getMessage()); } //helloStringItem = new StringItem("Bob",sb.toString()); response = new Ticker(sb.toString()); helloForm.setTicker(response); //response.setString(sb.toString()); } /** * This method should return an instance of the display. */ public Display getDisplay() { return Display.getDisplay(this); } /** * This method should exit the midlet. */ public void exitMIDlet() { getDisplay().setCurrent(null); destroyApp(true); notifyDestroyed(); } /** This method returns instance for helloForm component and should be called instead of accessing helloForm field directly. * @return Instance for helloForm component */ public Form get_helloForm() { if (helloForm == null) { // Insert pre-init code here helloForm = new Form(null, new Item[] { get_helloStringItem(), get_textField1() }); helloForm.addCommand(get_exitCommand()); helloForm.addCommand(get_okCommand1()); helloForm.setCommandListener(this); // Insert post-init code here } return helloForm; } /** This method returns instance for helloStringItem component and should be called instead of accessing helloStringItem field directly. * @return Instance for helloStringItem component */ public StringItem get_helloStringItem() { if (helloStringItem == null) { // Insert pre-init code here helloStringItem = new StringItem("Hello", "Hello, World!"); // Insert post-init code here } return helloStringItem; } /** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly. * @return Instance for exitCommand component */ public Command get_exitCommand() { if (exitCommand == null) { // Insert pre-init code here exitCommand = new Command("Exit", Command.EXIT, 1); // Insert post-init code here } return exitCommand; } /** This method returns instance for okCommand1 component and should be called instead of accessing okCommand1 field directly. * @return Instance for okCommand1 component */ public Command get_okCommand1() { if (okCommand1 == null) { // Insert pre-init code here okCommand1 = new Command("Ok", Command.OK, 1); // Insert post-init code here } return okCommand1; } /** This method returns instance for textField1 component and should be called instead of accessing textField1 field directly. * @return Instance for textField1 component */ public TextField get_textField1() { if (textField1 == null) { // Insert pre-init code here textField1 = new TextField("Username:", null, 120, TextField.ANY); // Insert post-init code here } return textField1; } public void startApp() { initialize(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }