import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.rms.*; import javax.microedition.io.*; import java.io.*; import java.util.*; public class Foorum extends MIDlet implements CommandListener { Form fMain = new Form("main"); Form fOutput = new Form("Out"); Form fMessage = new Form("Messages"); Command cOK = new Command("Continue", Command.OK, 1); Command cShow = new Command("Näita", Command.SCREEN, 1); Command cExit = new Command("Quit", Command.EXIT, 1); Command cMain = new Command("Algusesse", Command.EXIT, 1); String sMain[] = { cShow.getLabel() }; List lMain = new List("main", List.IMPLICIT, sMain, null); Display dThis = Display.getDisplay(this); String sURL = "http://silver.ultrasoft.ee/servlet/Foorum?action=mobile"; HTTP h; protected void startApp() throws MIDletStateChangeException {} protected void pauseApp() {} protected void destroyApp(boolean obligation) throws MIDletStateChangeException{} public Foorum() { fMain.append("Tere :)"); fMain.addCommand(cMain); fMain.addCommand(cShow); fMain.addCommand(cExit); fMain.setCommandListener(this); dThis.setCurrent(fMain); } public void commandAction (Command command, Displayable p2) { if (command == cShow) { fMessage.addCommand(cOK); fMessage.addCommand(cMain); fMessage.setCommandListener(this); h = new HTTP(); h.start(); try { Thread.sleep(10); } catch (Exception e) {} } else if (command == cExit) { notifyDestroyed(); } else if (command == cMain) { dThis.setCurrent(fMain); } } class HTTP extends Thread implements Runnable { public void run() { try { InputStream is = Connector.openInputStream(sURL); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i = is.read(); while(i != -1) { baos.write(i); i = is.read(); } String s = baos.toString(); String[] m = new String[255]; i = 0; int a = 0; while (true) { int j = a + 3; a = s.indexOf(":::", j); if (a == -1) break; m[i++] = s.substring((j == 3 ? 0 : j), a); } String[] sMessage = new String[i]; i = 0; while (m[i++] != null) sMessage[i - 1] = m[i - 1]; for (i = 0; i < sMessage.length; i++) { if (i % 3 == 0 && i != 0) fMessage.append("\n"); fMessage.append(sMessage[i]); } dThis.setCurrent(fMessage); } catch(Exception e) { e.printStackTrace(); } } } }