FileDocCategorySizeDatePackage
HttpMIDlet.javaAPI DocExample6458Tue Dec 04 22:27:20 GMT 2001ora.ch6

HttpMIDlet

public class HttpMIDlet extends javax.microedition.midlet.MIDlet implements javax.microedition.lcdui.CommandListener, Runnable

Fields Summary
private javax.microedition.lcdui.Display
display
private javax.microedition.lcdui.Form
addressForm
private javax.microedition.lcdui.Form
connectForm
private javax.microedition.lcdui.Form
displayForm
private javax.microedition.lcdui.TextField
serverURL
private javax.microedition.lcdui.StringItem
messageLabel
private javax.microedition.lcdui.StringItem
errorLabel
private javax.microedition.lcdui.Command
okCommand
private javax.microedition.lcdui.Command
exitCommand
private javax.microedition.lcdui.Command
backCommand
Constructors Summary
Methods Summary
public voidcommandAction(javax.microedition.lcdui.Command cmd, javax.microedition.lcdui.Displayable d)

        if (cmd == okCommand) {
            Thread t = new Thread(this);
            t.start();
            display.setCurrent(connectForm);
        } else if (cmd == backCommand) {
            display.setCurrent(addressForm);
        } else if (cmd == exitCommand) {
            try {
                destroyApp(true);
            } catch (MIDletStateChangeException ex) {
            }
            notifyDestroyed();
        }
    
protected voiddestroyApp(boolean unconditional)

    
private voidinitialize()

        display = Display.getDisplay(this);

        // Commands
        exitCommand = new Command("Exit", Command.EXIT, 0);
        okCommand = new Command("OK", Command.OK, 0);
        backCommand = new Command("Back", Command.BACK, 0);

        // The address form
        addressForm = new Form("HTTP Client");
        serverURL = new TextField("URL:", "", 256, TextField.ANY);
        addressForm.append(serverURL);
        addressForm.addCommand(okCommand);
        addressForm.addCommand(exitCommand);
        addressForm.setCommandListener(this);

        // The connect form
        connectForm = new Form("Connecting");
        messageLabel = new StringItem(null, "Connecting...\nPlease wait.");
        connectForm.append(messageLabel);
        connectForm.addCommand(backCommand);
        connectForm.setCommandListener(this);

        // The display form
        displayForm = new Form("Server Reply");
        messageLabel = new StringItem(null, null);
        displayForm.append(messageLabel);
        displayForm.addCommand(backCommand);
        displayForm.setCommandListener(this);
    
protected voidpauseApp()

    
public voidrun()

        InputStream is = null;
        HttpConnection conn = null;

        try {
            String url = serverURL.getString();
            if (!url.startsWith("http://") && 
			!url.startsWith("https://")) {
                url = "http://" + url;
            }
            conn = (HttpConnection)Connector.open(url, Connector.READ_WRITE);
        } catch (Exception ex) {
		System.out.println(ex);
		ex.printStackTrace();
            Alert alert = new Alert("Invalid Address",
                        "The supplied address is invalid\n" +
                        "Please correct it and try again.", null,
                        AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert, addressForm);
            return;
        }

        try {
            // Fetch the required page, reading up
            // to a maximum of 128 bytes
            if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
                is = conn.openInputStream();
                final int MAX_LENGTH = 128;
                byte[] buf = new byte[MAX_LENGTH];
                int total = 0;
                while (total < MAX_LENGTH) {
                    int count = is.read(buf, total, MAX_LENGTH - total);
                    if (count < 0) {
                        break;
                    }
                    total += count;
                }
                is.close();
                String reply = new String(buf, 0, total);
                messageLabel.setText(reply);
            } else {
                messageLabel.setText("Failed: error " + conn.getResponseCode() +
                                "\n" + conn.getResponseMessage());
            }
            
            // Get the response code and print all the headers
            System.out.println("Response code = " + conn.getResponseCode());
            System.out.println("Response message = " + conn.getResponseMessage());
            for (int i = 0; ; i++) {
                String key = conn.getHeaderFieldKey(i);
                String value = conn.getHeaderField(i);
                if (key == null) {
                    // Reached last header
                    break;
                }
                System.out.println(key + " = " + value);
            }
            conn.close();
            display.setCurrent(displayForm);
        } catch (IOException ex) {
		System.out.println(ex);
		ex.printStackTrace();
            Alert alert = new Alert("I/O Error",
                        "An error occurred while communicating with the server.",
                        null, AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert, addressForm);
            return;
        } finally {
            // Close open streams
           try {
                if (is != null) {
                    is.close();
                    is = null;
                }
            } catch (IOException ex1) {
            }
            try {
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (IOException ex1) {
            }
        }
    
protected voidstartApp()

        if (display == null) {
            initialize();
            display.setCurrent(addressForm);
        }