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

📄 httploadform.java

📁 一个反日货查询程序
💻 JAVA
字号:
/*
 * HttpLoadForm.java
 * 通过WEB下载产品清单的窗口界面
 * Created on 2004年2月4日, 下午8:05
 */

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

/**
 *
 * @author  wenyy
 * @version
 */
public class HttpLoadForm extends Form
{
    //命令对象
    private StringItem strUrl; //下载地址
    private StringItem strItem; //下载结果
    public String strResult = null;//保存下载的结果
    static public Command okCmd = new Command("完成",Command.SCREEN,1); //完成命令
    public HttpLoadForm() {
        super("下载清单");
        //添加地址
        strUrl =new StringItem("URL","http://www.vchelp.net/wyy/j2me/AntiJP.txt");
        strUrl.setLayout(Item.LAYOUT_NEWLINE_AFTER);
        append(strUrl);
        //设置结果显示窗口
        strItem = new StringItem("结果","");
        strItem.setPreferredSize(getWidth(), 90);//调整对象大小
        strItem.setLayout(Item.LAYOUT_VEXPAND | Item.LAYOUT_2);//调整对象布局方式
        append(strItem);
        //开始下载
        setTicker(new Ticker("正在下载..."));
        new Loader(this, strUrl.getText() );
    }
    //通过HTTP 下载清单,关于网络内容参考第9章内容
    private class Loader extends Thread {//通过WEB下载产品清单
        private String downloadUrl;
        private HttpLoadForm form; //在读入数据完成后停止窗口的滚动标题
        public Loader(HttpLoadForm f, String url) {
            form = f;
            downloadUrl = url;
            this.start();
        }
        public void run() {
            form.strResult = null;
            try {
                form.strResult = getUnicodeString(downloadUrl);
            }
            catch(Exception e) {
                System.out.println("下载失败,错误原因:"+e.getMessage());
                form.strItem.setText("下载失败,错误原因:"+e.getMessage());
            }
            form.setTicker(null); //停止滚动条
            if( form.strResult != null)
                form.strItem.setText("下载成功");
            //添加退出命令
            addCommand(okCmd);
        }
        //通过 HttpConnection 对象来获取网页内容, 参考 9.3.10 节内容
        public String getUnicodeString(String url) throws IOException ,Exception {
            HttpConnection  c = null;
        InputStream is  = null;
        DataInputStream dis = null;
        String str=null;
        try
        {
                c = (HttpConnection)Connector.open(url); //创建 HttpConnection 对象
                c.setRequestProperty("User-Agent","MIDP/2.0");//设置客户方名称
                c.setRequestProperty("Accept","text/html, text/plain, text/xml, image/png, */*");//设置可接受的MIME类型
                
                int rc  = c.getResponseCode(); //转换到Connected 状态
                if( rc != HttpConnection.HTTP_OK) {
                    throw new Exception(c.getResponseMessage());
                }
                is  = c.openInputStream();//打开输入流
                dis = new DataInputStream(is); 
                char ch = dis.readChar();//丢弃文件的第一个Unicode 字符
                boolean unicodeBig = true;
                if(ch !='\uFEFF') //判断是否为 Unicode Big endian 编码方式
                    unicodeBig = false;
                StringBuffer sb = new StringBuffer();
                int bHigh,bLow;

                while(dis.available() >=2 ) {//逐个读入字符
                    if(! unicodeBig) {
                        bHigh=dis.read();
                        bLow=dis.read();
                        ch=(char)bLow;
                        ch <<= 8;
                        ch |= bHigh;
                        sb.append(ch);
                    }
                    else{
                        sb.append(dis.readChar());
                }
            }
                str = sb.toString(); //得到读入的字符串
                sb = null;
            }
            catch (IOException e) {
                System.out.println("下载失败,错误原因:"+e.getMessage());
                form.strItem.setText("下载失败,错误原因:"+e.getMessage());
            }
        finally {
                if (dis != null) dis.close();
            if (is != null) is.close();
            if (c != null) c.close();
        }
        return str;
        }
    }
}

⌨️ 快捷键说明

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