HttpConnection: getQuery() : HttpConnection « javax.microedition.io « Java by API






HttpConnection: getQuery()

 

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 ConnectHttpMIDlet 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://www.g.com/p=MDIP&u=b&y=y&hc=0&hs=0";

  private Form mainForm, resultForm;

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

  private StringItem resultItem;

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

  public void startApp() {
    mainForm = new Form("Data");
    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 = "failed";
      }
      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;
    String content = "";
    try {
      hpc = (HttpConnection) Connector.open(URLString);

      content += "URL: " + hpc.getURL() + "\n";
      content += "Protocol: " + hpc.getProtocol() + "\n";
      content += "Host: " + hpc.getHost() + "\n";
      content += "Port: " + hpc.getPort() + "\n";
      content += "File: " + hpc.getFile() + "\n";
      content += "Query: " + hpc.getQuery() + "\n";
      content += "reference: " + hpc.getRef() + "\n";
      content += "request method: " + hpc.getRequestMethod();

      if (hpc != null)
        hpc.close();

    } catch (IOException e2) {
    }

    return content;
  }
}

   
  








Related examples in the same category

1.HttpConnection.HTTP_OK
2.HttpConnection: getFile()
3.HttpConnection: getHeaderField(String value) throws IOException
4.HttpConnection: getPort()
5.HttpConnection: getProtocol()
6.HttpConnection: getRef()
7.HttpConnection: getRequestMethod()
8.HttpConnection: getURL()
9.HttpConnection: setRequestProperty(String key, String value) throws IOException
10.InputConnection: openInputStream() throws IOException