课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——多线程网页获取方法


  共有14173人关注过本帖树形打印复制链接

主题:程序代码——多线程网页获取方法

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——多线程网页获取方法  发帖心情 Post By:2008/10/28 20:21:20 [只看该作者]

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.ContentConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet implements CommandListener {
        Display display;
        Form form = new Form("练习");
        TextField tf;
        Command ok = new Command("开始", Command.OK, 1);
        Command cancel = new Command("取消", Command.OK, 1);
        Command back = new Command("再来", Command.BACK, 1);
        Alert info = new Alert("消息", "正在下载...", null, AlertType.INFO);
        TextBox tb = new TextBox("网页文本", "", 100, TextField.ANY);
        boolean cacncelFlag = false;

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {
                tf = new TextField("URL:", "", 50, TextField.ANY);
                form.append(tf);
                form.addCommand(ok);
                form.setCommandListener(this);
                display.setCurrent(form);
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }

        public void commandAction(Command com, Displayable disp) {
                if (com == ok) {
                        cacncelFlag = false;
                        Downloader downloader = new Downloader(tf.getString(), this);
                        Thread thread = new Thread(downloader);
                        thread.start();
                        info.setTimeout(10000);
                        info.addCommand(cancel);
                        info.setCommandListener(this);
                        display.setCurrent(info);
                } else if (com == cancel) {
                        cacncelFlag = true;
                        display.setCurrent(form);
                } else if (com == back) {
                        display.setCurrent(form);
                }
        }

        public void setResults(String str) {
                tb.setMaxSize(str.length());
                tb.setString(str);
                tb.addCommand(back);
                tb.setCommandListener(this);
                display.setCurrent(tb);
        }

        public boolean isCacncelFlag() {
                return cacncelFlag;
        }

}

class Downloader implements Runnable {
        String url;
        Exec midlet;

        public Downloader(String url, Exec midlet) {
                this.url = url;
                this.midlet = midlet;
        }

        public void run() {
                ContentConnection con = null;
                InputStream is = null;
                try {
                        con = (ContentConnection) Connector.open(url);
                        is = con.openInputStream();
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] dataArray = null;
                        int ch = 0;
                        while ((ch = is.read()) != -1 && !midlet.isCacncelFlag()) {
                                baos.write(ch);
                        }
                        if (!midlet.isCacncelFlag()) {
                                dataArray = baos.toByteArray();
                                midlet.setResults(new String(dataArray));
                        }

                } catch (Exception e) {
                        System.out.println(e.getMessage());

                } finally {
                        if (is != null)
                                try {
                                        is.close();
                                } catch (IOException e) {
                                        System.out.println(e.getMessage());
                                }
                        if (con != null)
                                try {
                                        con.close();
                                } catch (IOException e) {
                                        System.out.println(e.getMessage());
                                }
                }
        }

}

[此贴子已经被作者于2010-12-12 18:43:40编辑过]

 回到顶部