Response Header : HttpConnection « J2ME « Java Tutorial






import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class ResponseHeaderMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);

  private Command sendCommand = new Command("Send", Command.OK, 1);

  private Command backCommand = new Command("Upload", Command.OK, 1);

  private Display display;

  private String defaultURL = "http://localhost/";

  private Form mainForm, resultForm;

  private TextField URL;

  private DateField DateField1 = new DateField("Date", DateField.DATE_TIME);

  private DateField DateField2 = new DateField("Expiration", DateField.DATE_TIME);

  private DateField DateField3 = new DateField("Last Modified", DateField.DATE_TIME);

  private StringItem resultItem;

  public ResponseHeaderMIDlet() {
    display = Display.getDisplay(this);
  }

  public void startApp() {
    URL = new TextField(null, defaultURL, 250, TextField.URL);
    mainForm = new Form("Test");
    mainForm.append(URL);
    mainForm.addCommand(sendCommand);
    mainForm.addCommand(exitCommand);
    mainForm.setCommandListener(this);
    display.setCurrent(mainForm);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable s) {
    if (c == sendCommand) {
      String result = "";
      resultItem = new StringItem(null, result);
      resultForm = new Form("");
      String URLString = URL.getString();

      try {
        result = requestUsingGET(URLString);
      } catch (IOException e) {
        result = "Falied";
      }

      resultItem.setText(result);

      resultForm.append(resultItem);
      resultForm.append(DateField1);
      resultForm.append(DateField2);
      resultForm.append(DateField3);
      resultForm.addCommand(backCommand);
      resultForm.setCommandListener(this);
      display.setCurrent(resultForm);
    } else if (c == backCommand) {
      URL.setString(defaultURL);
      display.setCurrent(mainForm);
    } else {
      destroyApp(false);
      notifyDestroyed();
    }
  }
  private String requestUsingGET(String URLString) throws IOException {
    HttpConnection hpc = null;

    String content = "";
    try {
      hpc = (HttpConnection) Connector.open(URLString);

      int status = hpc.getResponseCode();
      if (status != HttpConnection.HTTP_OK)
        content = "Falied";
      else {
        DateField1.setDate(new java.util.Date(hpc.getDate()));
        DateField2.setDate(new java.util.Date(hpc.getExpiration()));
        DateField3.setDate(new java.util.Date(hpc.getLastModified()));

        content += "Content Type: " + hpc.getType() + "\n";
        content += "Content Length: " + hpc.getLength() + "\n";
        content += "Contnet Encoding: " + hpc.getEncoding() + "\n";
      }
      if (hpc != null)
        hpc.close();

    } catch (IOException e2) {
    }

    return content;
  }
}








31.37.HttpConnection
31.37.1.An example MIDlet to fetch a page using an HttpConnection
31.37.2.Use HttpConnection
31.37.3.An example MIDlet to invoke a CGI script (POST method is used).
31.37.4.Load Image with HttpConnection
31.37.5.Response Header