/** * The following MIDlet application demonstrates how to establish a * HttpConnection and uses it to send a GET request to Web server. */ /************************************************* THIS CODE CAUSES A WARNING! WHY? **************************************************/ // include MIDlet class libraries import javax.microedition.midlet.*; // include networking class libraries import javax.microedition.io.*; // include GUI class libraries import javax.microedition.lcdui.*; // include I/O class libraries import java.io.*; public class HttpGET extends MIDlet implements CommandListener { // A default URL is used. User can change it from the GUI. private static String defaultURL = "http://www.brunel.ac.uk/~emstaam/home.htm"; // GUI components for entering a Web URL. private Display myDisplay = null; private Form mainScreen; private TextField requestField; // GUI components for displaying server responses. private Form resultScreen; private StringItem resultField; // the "send" button used on mainScreen Command sendCommand = new Command("SEND", Command.OK, 1); // the "back" button used on resultScreen Command backCommand = new Command("BACK", Command.OK, 1); // Exit MIDlet Command Command cmExit = new Command("Exit", Command.EXIT, 1); public HttpGET(){ // initialize the GUI components myDisplay = Display.getDisplay(this); mainScreen = new Form("Type in a URL:"); requestField = new TextField(null, defaultURL, 100, TextField.URL); mainScreen.append(requestField); mainScreen.addCommand(sendCommand); mainScreen.addCommand(cmExit); mainScreen.setCommandListener(this); } public void startApp() { myDisplay.setCurrent(mainScreen); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { // when user clicks on the "send" button on mainScreen if (c == sendCommand) { // retrieve the Web url that user entered String urlstring = requestField.getString(); // send a GET request to Web server String resultstring = ""; try { resultstring = sendGetRequest(urlstring); } catch (IOException e) { resultstring = "ERROR"; } // display the page content retrieved from Web server resultScreen = new Form("GET Result:"); resultField = new StringItem(null, resultstring); resultScreen.append(resultField); resultScreen.addCommand(backCommand); resultScreen.setCommandListener(this); myDisplay.setCurrent(resultScreen); } else if (c == backCommand) { // do it all over again requestField.setString(defaultURL); myDisplay.setCurrent(mainScreen); } else if(c ==cmExit) { destroyApp(true); notifyDestroyed(); } } // send a GET request to Web server public String sendGetRequest(String urlstring) throws IOException { HttpConnection hc = null; DataInputStream dis = null; String message = ""; try { // open up an HttpConnection with the Web server // the default request method is GET. hc = (HttpConnection) Connector.open(urlstring); // obtain a DataInputStream from the HttpConnection dis = new DataInputStream(hc.openInputStream()); // retrieve the contents of the requested page from Web server int ch; while ((ch = dis.read()) != -1) { message = message + (char) ch; } } finally { if (hc != null) hc.close(); if (dis != null) dis.close(); } return message; } }