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

📄 httpservletscreen.java

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

import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

/**
 * HTTP内容信息显示类
 */
public class HttpServletScreen
    extends Form
    implements CommandListener {

  /** 显示类构造器 */
  public HttpServletScreen(HTTPServletMIDlet httpServletMIDlet) {
    super("Displayable Title");
    this.httpServletMIDlet = httpServletMIDlet;
    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));
  }

  class HttpConnector
      implements java.lang.Runnable {

    //线程主方法
    public void run() {
      connect();
    }

  }

  /**
   *   创建HTTP连接核心方法
   */
  public void connect() {
    byte[] buffer = null;
    try {
      HttpConnection connection = (HttpConnection) Connector.open(
          "http://yourcomputer.com/ midp/httpconnectservlet?userName=user1&password=123 ");
      InputStream is = null;
      is = connection.openInputStream();
      int len = (int) connection.getLength();
      System.out.println(len);
      DataInputStream dis = new DataInputStream(is);
      int actual = 0;
      int bytesread = 0;
      buffer = new byte[len];
      while ( (bytesread != len) && (actual != -1)) {
        actual = is.read(buffer, bytesread, len - bytesread);
        bytesread += actual;
      }
      dis.close();
      is.close();
      connection.close();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
    TextBox input = new TextBox("Website content:", "", 50000, TextField.ANY);
    input.setTicker(new Ticker("connect internet"));
    input.setString(new String(buffer));
    buffer = null;
    Display.getDisplay(httpServletMIDlet).setCurrent(input);
  }

  /**
   *  处理命令事件,进行相应的分支操作
   */
  public void commandAction(Command command, Displayable displayable) {

    if (command.getCommandType() == Command.EXIT) {
      // 退出MIDlet
      HTTPMIDlet.quitApp();
    }
    else if (command.getCommandType() == Command.OK) {
      new java.lang.Thread(new HttpConnector()).start();
    }
  }

  HTTPServletMIDlet httpServletMIDlet = null;
}

⌨️ 快捷键说明

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