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

📄 httpmidlet.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 HttpMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  private Command sendCommand;
  private Command backCommand;
    //The display for this MIDlet
  private Display display;


  private String URL = "http://localhost/JIDCA/JavaPowered-8.png";
  //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 Item mItem;//联机成功时作为ImageItem,失败时作为StringItem
  private TextField URLField;

  public HttpMIDlet() {
    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() {
	URLField = new TextField(null,URL,250,TextField.URL);
    mainForm = new Form("请输入网址");
    mainForm.append(URLField);
    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){
		  Image aImage=null;

		  resultForm = new Form("图片");
		  String URLString = URLField.getString();
          Form mainForm = new Form("载入中...");

          // Do network loading in a separate thread.

                try {

                  Image image = loadImage(URLString);
                  mItem = new ImageItem(null, image, 0, null);
                }
                catch (IOException ioe) {
                  mItem = new StringItem(null, ioe.toString());
                }
                resultForm.append(mItem);
                resultForm.addCommand(backCommand);
				resultForm.setCommandListener(this);
		        display.setCurrent(resultForm);
   }

	  else if(c==backCommand){
		  URLField.setString(URL);
		  display.setCurrent(mainForm);
	  }
	  else{
          destroyApp(false);
          notifyDestroyed();
	  }
  }




  public Image loadImage(String url) throws IOException {
      HttpConnection hpc = null;
      DataInputStream dis = null;
      try {
  	  hpc = (HttpConnection)Connector.open(url);
        int length = (int)hpc.getLength();
        byte[] data = new byte[length];
        dis = new DataInputStream(hpc.openInputStream());
        dis.readFully(data);
        return Image.createImage(data, 0, data.length);
      }
      finally {
        if(hpc != null) hpc.close();
        if(dis != null) dis.close();
      }
  }
}

⌨️ 快捷键说明

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