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

📄 httpscreen.java

📁 手机游戏设计一书的源代码
💻 JAVA
字号:
package net;

/**
*HTTP站点内容显示
*/
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class HttpScreen extends Form implements CommandListener {

/**
*初始化
*/
  public HttpScreen(HTTPMIDlet httpMIDlet) {
    super("Displayable Title");
    this.httpMIDlet = httpMIDlet;
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  /**
*具体的初始化的实现方法
*/
  private void jbInit() throws Exception {
    setCommandListener(this);
    addCommand(new Command("Exit", Command.EXIT, 1));
    addCommand(new Command("Connect", Command.OK, 1));
   }

 /**
*  HTTP连接核心方法,通过调用通用连接框架中的Connector.open()方法
*  建立移动信息设备与服务器的HTTP连接
*/
   public void connect () {
     byte buffer[] = new byte[128];
     try {
      HttpConnection connection = (HttpConnection)Connector.open("http://java.sun.com",Connector.READ);
      InputStream is = connection.openInputStream();
      DataInputStream dis = new DataInputStream(is);
      dis.read(buffer,0,128);
      dis.close();
      is.close();
      connection.close();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }

      TextBox input = new TextBox("Website content:", "", 5000, TextField.ANY);
      input.setTicker(new Ticker("connect internet"));
      input.setString(new String(buffer));
      buffer = null;
      Display.getDisplay(httpMIDlet).setCurrent(input);
   }


class HttpConnector implements java.lang.Runnable {
     public void run() {
       connect();
     }
   }


/**
*处理命令事件,进行相应的操作
*/
  public void commandAction(Command command, Displayable displayable) {
    if (command.getCommandType() == Command.EXIT) {
      HTTPMIDlet.quitApp();
    }else if(command.getCommandType() == Command.OK) {

      new Thread(new HttpConnector()).start();

    }
  }

HTTPMIDlet httpMIDlet = null;
}

⌨️ 快捷键说明

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