import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.rms.*; import javax.microedition.io.*; import java.io.*; import java.util.*; public class Turism extends MIDlet implements CommandListener { Form fMain = new Form("main"); Form fOutput = new Form("Out"); Command cMK = new Command("Vali maakond", Command.SCREEN, 1); Command cOK = new Command("Continue", Command.OK, 1); Command cExit = new Command("Quit", Command.EXIT, 1); Command cMain = new Command("Algusesse", Command.EXIT, 1); String sMain[] = { cMK.getLabel() }; List lMain = new List("main", List.IMPLICIT, sMain, null); List lMK = new List("maakonnad", List.IMPLICIT); Display dThis = Display.getDisplay(this); String sURL = "http://silver.ultrasoft.ee/servlet/Turism?action=mobile"; HTTP h; String show = "ALL"; protected void startApp() throws MIDletStateChangeException {} protected void pauseApp() {} protected void destroyApp(boolean obligation) throws MIDletStateChangeException{} public Turism() { fMain.append("Tere :)"); fMain.addCommand(cMK); fMain.addCommand(cMain); fMain.addCommand(cExit); fMain.setCommandListener(this); dThis.setCurrent(fMain); } public void commandAction (Command command, Displayable p2) { if (command == cMK || command == cOK) { lMK.addCommand(cOK); lMK.addCommand(cMain); lMK.setCommandListener(this); if (dThis.getCurrent() == lMK) { int sel = lMK.getSelectedIndex(); show = sel == -1 ? "ALL" : lMK.getString(sel); } h = new HTTP(); h.start(); try { Thread.sleep(10); } catch (Exception e) {} } /* else if (command == cOK) { if (dThis.getCurrent() == lMK) show = lMK.getString(lMK.getSelectedIndex()); } */ else if (command == cExit) { notifyDestroyed(); } else if (command == cMain) { dThis.setCurrent(fMain); } } public void show(Displayable d, Command c1, Command c2) { d.addCommand(c1); d.addCommand(c2); d.setCommandListener(this); dThis.setCurrent(d); } class HTTP extends Thread implements Runnable { public void run() { try { InputStream is = Connector.openInputStream(sURL + "&show=" + show); 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[50]; i = 0; int a = 0; while (true) { int j = a + 1; a = s.indexOf("\n", j); if (a == -1) break; String toAdd = s.substring((j == 1 ? 0 : j), a); boolean found = false; for (int b = 0; b < m.length; b++) { if (m[b] == null) break; if (m[b].compareTo(toAdd) == 0) { found = true; break; } } if (!found) m[i++] = toAdd; } String[] sMK = new String[i]; i = 0; while (m[i++] != null) sMK[i - 1] = m[i - 1]; lMK.deleteAll(); for (i = 0; i < sMK.length; i++) lMK.append(sMK[i], null); dThis.setCurrent(lMK); } catch(Exception e) { e.printStackTrace(); } } } }