⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpheadmidlet.java

📁 个很好的用来学习学习手机通信的例子
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class HttpHEADMIDlet extends MIDlet
implements CommandListener {
  private Command exitCommand;
  private Command sendCommand;
  private Command backCommand;
    //The display for this MIDlet
  private Display display;

  private String defaultURL = "http://localhost/JIDCA/";
  private Form mainForm,resultForm;
  private TextField URL;
  private StringItem resultItem;

  public HttpHEADMIDlet() {
    display = Display.getDisplay(this);
    exitCommand = new Command("离开",Command.EXIT,1);
    sendCommand = new Command("送出",Command.OK, 1);
    backCommand = new Command("上页",Command.OK,1);
   }

  // Start the MIDlet by creating the TextBox and
  // associating the exit command and listener.
  public void startApp() {
	URL = new TextField(null,defaultURL,250,TextField.URL);
    mainForm = new Form("HEDA Method");
    mainForm.append(URL);
    mainForm.addCommand(sendCommand);
    mainForm.addCommand(exitCommand);
    mainForm.setCommandListener(this);
    display.setCurrent(mainForm);
  }

  // Pause is a no-op because there are no background
  // activities or record stores to be closed.
  public void pauseApp() { }

  // Destroy must cleanup everything not handled
  // by the garbage collector.
  // In this case there is nothing to cleanup.
  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 = "联机失败!";
		  }

		  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;
	  DataOutputStream dos = null;

      boolean newline = false;
	  String content = "";
	  try{
		  hpc = (HttpConnection)Connector.open(URLString);
		  hpc.setRequestMethod(HttpConnection.HEAD);

		  dis = new DataInputStream(hpc.openInputStream());
          int i = 1;
          String key = "";
          String value = "";

		  while((value = hpc.getHeaderField(i)) != null){
			  key = hpc.getHeaderFieldKey(i++);
			  content += key + ":" + value + "\n";
		  }
	  }
	  catch(IOException e){
		  System.out.println("error");
	  }

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

	  return content;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -