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

📄 httpunicode.java

📁 J2ME MIDP 2.0 无线设备编程的一些源码
💻 JAVA
字号:
//HttpUnicode.java
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class HttpUnicode extends MIDlet implements CommandListener
{
    //命令对象
    private Command exitCommand, loadCmd;
    private Form form;
    private StringItem strItem;
    private TextField tf; //通过TextBox来显示信息
    private Ticker tker; //访问网络时显示滚动的标题
    public HttpUnicode()
    {
        tker = new Ticker("Loading");
        form = new Form("unicodeLoad");
        tf =new TextField("URL","http://localhost/j2me.txt",120,TextField.URL);
        tf.setLayout(Item.LAYOUT_NEWLINE_AFTER);
        strItem = new StringItem("结果","");
        strItem.setPreferredSize(form.getWidth(), 90);//调整对象大小
        strItem.setLayout(Item.LAYOUT_VEXPAND | Item.LAYOUT_2);//调整对象布局方式
        form.append(tf);
        form.append(strItem);

        exitCommand =new Command("Exit",Command.EXIT,1);
        loadCmd =new Command("LoadUnicode",Command.SCREEN,1);
        form.addCommand(exitCommand);
        form.addCommand(loadCmd);
        form.setCommandListener(this);
    }
    protected void startApp(  ) throws MIDletStateChangeException
    {
        Display.getDisplay(this).setCurrent(form);
    }

    protected void pauseApp(  )
    {
    }

    protected void destroyApp( boolean p1 )
    {
    }

    public void commandAction(Command c,Displayable d)
    {
        if (c ==exitCommand)
        {
            destroyApp(false);
            notifyDestroyed();
        }
        else if(c ==loadCmd)
        {
            form.setTicker(tker);
            new Loader(form, strItem, tf.getString());
        }
    }
private class Loader extends Thread
{
    private String testUrl;
    private Form form; //在读入数据完成后停止窗口的滚动标题
    private StringItem strItem; //设置StringItem 中的文字
    public Loader(Form f, StringItem i, String url)
    {
        form = f;
        strItem = i;
        testUrl = url;
        this.start();
    }
    public void run()
    {
        String strLoad=null;
        try 
        {
            strLoad = getUnicodeString(testUrl);
        }
        catch(Exception e)
        {System.out.println("error = "+e.getMessage());}   
        form.setTicker(null);
        if( strLoad != null)
            strItem.setText(strLoad);//设置文字
    }

    public String getUnicodeString(String url) throws IOException ,Exception
    {//通过 HttpConnection 对象来获取网页内容
        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("error = "+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 + -