Http Get with J2ME : Connector « J2ME « Java Tutorial






import java.io.DataInputStream;
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.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 HttpGETMIDlet 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/JIDCA/hello.asp?name=JIDCA";

  private Form mainForm, resultForm;

  private TextField URL = new TextField(null, defaultURL, 250, TextField.URL);

  private StringItem resultItem;

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

  public void startApp() {
    mainForm = new Form("Address");
    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("Result");
      String URLString = URL.getString();

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

      resultItem.setText(result);
      resultForm.append(resultItem);
      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;
    DataInputStream dis = null;
    boolean newline = false;
    String content = "";
    try {
      hpc = (HttpConnection) Connector.open(URLString);
      dis = new DataInputStream(hpc.openInputStream());
      int character;

      while ((character = dis.read()) != -1) {
        if ((char) character == '\\') {
          newline = true;
          continue;
        } else {
          if ((char) character == 'n' && newline) {
            content += "\n";
            newline = false;
          } else if (newline) {
            content += "\\" + (char) character;
            newline = false;
          } else {
            content += (char) character;
            newline = false;
          }
        }

      }
      if (hpc != null)
        hpc.close();
      if (dis != null)
        dis.close();
    } catch (IOException e2) {
    }

    return content;
  }
}








31.40.Connector
31.40.1.Image Loader
31.40.2.URL query
31.40.3.Call asp page from J2ME
31.40.4.Http Get with J2ME
31.40.5.Http header
31.40.6.Load image from a URL
31.40.7.Check Http connection returning code
31.40.8.Http post