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

📄 securitymidlet.java

📁 <j2me 开发精解> 詹建光著 里所有的源码。对J2me的开发相当有帮助
💻 JAVA
字号:
package com.j2medev.ch7.security;

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

public class SecurityMIDlet extends MIDlet implements CommandListener, Runnable {
    private Display display = null;
    private Form mainForm = null;
    private Displayable resume = null;
    private String serverURL = null;
    private TextField address = new TextField("输入网址:", "http://java.sun.com", 30, TextField.ANY);
    public static final Command okCommand = new Command("连接",Command.OK, 2);
    public static final Command exitCommand = new Command("退出",Command.EXIT, 1);
    public static final Command backCommand = new Command("返回", Command.BACK, 1);
    
    public SecurityMIDlet() {
        mainForm = new Form("安全模型");
        mainForm.append(address);
        mainForm.addCommand(exitCommand);
        mainForm.addCommand(okCommand);
        mainForm.setCommandListener(this);
    }
    
    public void startApp() {
        if (display == null){
            display = Display.getDisplay(this);
            resume = mainForm;
        }
        display.setCurrent(resume);
    }
    
    public void pauseApp() {
        resume = display.getCurrent();
    }
    
    public void destroyApp(boolean unconditional) {
        
    }
    //响应用户输入
    public void commandAction(Command cmd, Displayable displayable) {
        if (cmd == exitCommand ){
            notifyDestroyed();
        }else if(cmd == backCommand){
            display.setCurrent(mainForm);
        } else if (cmd == okCommand) {
            serverURL = address.getString();
            //设置等待界面
            Form waitForm = new Form("正在连接网络...");
            waitForm.append("联网中,请稍候......");
            display.setCurrent(waitForm);
            Thread t = new Thread(this);
            t.start();
        }
    }
    public void run() {
        Form resultsForm = new Form("返回结果");
        resultsForm.addCommand(backCommand);
        resultsForm.setCommandListener(this);
        
        HttpConnection hc = null;
        InputStream in = null;
        try {
            //连接网络
            hc = (HttpConnection)Connector.open(serverURL);
            int code = hc.getResponseCode();
            if(code != HttpConnection.HTTP_OK){
                resultsForm.append("服务器无响应!");
                display.setCurrent(resultsForm);
                return;
            }
            in = hc.openInputStream();
            //读取返回内容,这里只读取256个字节
            int length = 256;
            byte[] raw = new byte[length];
            int readLength = in.read(raw);
            String message = new String(raw, 0, readLength);
            resultsForm.append(message);
        } catch (Exception e) {
            resultsForm.append(new StringItem("出现异常:", e.toString()));
        } finally {
            //关闭流和连接
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ioe) {
                }
            }
            if (hc != null) {
                try {
                    hc.close();
                } catch (IOException ioe) {
                }
            }
        }
        display.setCurrent(resultsForm);
    }
}

⌨️ 快捷键说明

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