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

📄 httpimage.java

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

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

        exitCommand =new Command("Exit",Command.EXIT,1);
        loadCmd =new Command("LoadImage",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, imgItem, tf.getString());
        }
    }
private class Loader extends Thread
{
    private String testUrl;
    private Form form; //在读入数据完成后停止窗口的滚动标题
    private ImageItem imgItem; //设置ImageItem 中的图像
    public Loader(Form f, ImageItem i, String url)
    {
        form = f;
        imgItem = i;
        testUrl = url;
        this.start();
    }
    public void run()
    {
        Image imgLoad=null;
        try 
        {
            imgLoad = getPngImage(testUrl);
        }
        catch(Exception e)
        {System.out.println("error = "+e.getMessage());}   
        form.setTicker(null);
        if( imgLoad != null)
            imgItem.setImage(imgLoad);//设置图像
    }

    public Image getPngImage(String url) throws IOException ,Exception
    {//通过 HttpConnection 对象来获取网页内容
        HttpConnection  c = null;
        InputStream is  = null;
        Image img=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();//打开输入流
            img = Image.createImage(is); //通过输入流创建图像
        }
        catch (IOException e) 
        {
            System.out.println("error = "+e.getMessage()); 
        }
        finally
        {
            if (is != null)
                is.close();
            if (c != null)
                c.close();
        }
        return img;
    }
}

}

⌨️ 快捷键说明

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