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

📄 getdemo.java

📁 语言:英文 主要是介绍企业J2ME开发
💻 JAVA
字号:
/*
 * GetDemo.java
 *
 * Created on 2005年3月29日, 上午10:18
 */

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

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

/**
 *
 * @author  Liu Bin
 * @version
 */
public class GetDemo extends MIDlet implements CommandListener {
    private Display display;
    
    private Form form;
    private TextField tfURL;
    
    //定义使用的命令按钮
    private Command cmdExit;
    private Command cmdConnect;
    
    public GetDemo() {
        try {
            jbInit();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    private void jbInit() throws Exception {
        cmdExit = new Command("退出", Command.EXIT, 0);
        cmdConnect = new Command("连接", Command.SCREEN, 1);
        tfURL = new TextField("Please Input URL",
                "http://127.0.0.1:8080",80,TextField.URL);
        form = new Form("Httpconnection GET test");
        form.append(tfURL);
        form.addCommand(cmdExit);
        form.addCommand(cmdConnect);
        form.setCommandListener(this);
    }
    
    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(form);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command cmd, Displayable d) {
        if (cmd == cmdExit) {
            destroyApp(true);
        } else if (cmd == cmdConnect) {
            try {
                String url = tfURL.getString();
                if (!url.startsWith("http://")) {
                    url = "http://" + url;
                }
                String ret = requestGet(url);
                out(ret);
            } catch(Exception e) {
                System.out.println("发生异常: " + e.toString());
                form.append("发生异常: " + e.toString());
            }
        }
    }
    
    /**
     * HttpConnection GET功能
     * <p>
     * @Param url HTTP地址
     */
    public String requestGet(String url) throws IOException{
        
        HttpConnection hpc = null;
        DataInputStream dis = null;
        String content = "";
        
        try{
            // 建立连接
            hpc = (HttpConnection)Connector.open(url);
            out("创建HttpConnection成功\n");
            hpc.setRequestMethod(HttpConnection.GET);
            out("setRequestMethod成功\n");
            dis =  new DataInputStream(hpc.openInputStream());
            out("打开InputStream成功\n");
            
            if (hpc.getResponseCode() == HttpConnection.HTTP_OK) {
                int character;
                
                // 读取返回的HTTP内容
                out("正在读取数据...\n");
                while((character = dis.read()) != -1){
                    content +=(char)character;
                }
            }
        } catch(IOException e){
            out("读取数据发生异常:" + e.toString());
        } finally{
            if(hpc != null){
                hpc.close();
                hpc = null;
            }
            if(dis != null){
                dis.close();
                dis = null;
            }
        }
        
        content = (unicodeTogb2312(content)).trim();
        return content;
    }
    
    // ===============================================================
    // 由于内容可能有中文,所以在接受到信息后要对内容进行字符集的转换
    // ===============================================================
    public static String unicodeTogb2312(String s){
        if (s==null){
            return "";
        }
        
        if (s.equals("")){
            return s;
        }
        
        try{
            return new String(s.getBytes("ISO8859_1"),"gb2312");
        } catch(Exception uee){
            return s;
        }
    }
    
    private void out(String msg) {
        System.out.print(msg);
        form.append(msg);
    }
    
}

⌨️ 快捷键说明

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