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

📄 httpgetmidlet.java

📁 一组http交互程序 ConnectHttpMIDlet HttpCookieMIDlet.java HttpGETMIDlet.java HttpHEADMIDlet.java Ht
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class HttpGETMIDlet 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/hello.asp?name=JIDCA";
  //private String defaultURL = "http://www.webappcabaret.com/jidca";
  //private String defaultURL ="http://home.pchome.com.tw/online/jidca/index.html";
  private Form mainForm,resultForm;
  private TextField URL;
  private StringItem resultItem;

  public HttpGETMIDlet() {
    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("请输入网址");
    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;
      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;
				 }
			  }

		  }
	  }
	  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 + -