| 在我们联网的时候同样还是要在另外一个线程进行,为了提高效率我们使用wait()和notify()来调度线程,线程启动后会进入wait()的状态,因为我们在midlet对象上调用了wait()。当用户按了Connect Command后我们调用midlet.notify()来让线程继续运行。 if (cmd == connCommand) { synchronized (this) { notify(); } } else if (cmd == exitCommand) { exitMIDlet(); }
synchronized (midlet) { try { midlet.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("connect to server...");
当读取Image文件的时候,我们建立连接后就可以得到InputStream的实例了,接收数据显得比较重要。我采用的方法是新建一个ByteArrayOutputStream实例baos,然后通过read()得到字节首先写入到baos里面去。传输结束后通过baos.toByteArray()得到Image的字节数据,这样我们就可以很容易的构建出图片来了,最后把它显示在Form里面。 httpConn = (HttpConnection) Connector.open(URL); is = httpConn.openInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int ch = 0; while ((ch = is.read()) != -1) { baos.write(ch); } byte[] imageData = baos.toByteArray(); Image image = Image.createImage(imageData, 0, imageData.length); midlet.setImage(image); baos.close(); is.close(); httpConn.close();  首先你应该在web服务器上放一个大小适中的png格式的图片,然后运行本程序进行联网,这样就可以通过http协议传输图片了。(如上图)
下面是源代码
import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.lcdui.*; import java.io.*; import javax.microedition.io.*;public class ImageGetter extends MIDlet implements CommandListener { private Display display; public static final Command connCommand = new Command("Connect", Command.ITEM, 1); public static final Command exitCommand = new Command("Exit", Command.EXIT,1); private Form mainForm; private GetterThread gt;
protected void startApp() throws MIDletStateChangeException { display = Display.getDisplay(this); mainForm = new Form("Image Getter"); mainForm.append("Click Connect to get Image"); mainForm.addCommand(connCommand); mainForm.addCommand(exitCommand); mainForm.setCommandListener(this); display.setCurrent(mainForm); gt = new GetterThread(this); gt.start(); } public void setImage(Image image) { mainForm.append(image); display.setCurrent(mainForm); }
protected void pauseApp() { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } public void commandAction(Command cmd, Displayable disp) { if (cmd == connCommand) { synchronized (this) { notify(); } } else if (cmd == exitCommand) { exitMIDlet(); } } private void exitMIDlet() { try { destroyApp(false); notifyDestroyed(); } catch (MIDletStateChangeException e) { e.printStackTrace(); } } class GetterThread extends Thread { private ImageGetter midlet; public static final String URL = "http://localhost/j2medev.png"; private HttpConnection httpConn = null; private InputStream is = null; public GetterThread(ImageGetter midlet) { this.midlet = midlet; } public void run() { synchronized (midlet) { try { midlet.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("connect to server..."); try { httpConn = (HttpConnection) Connector.open(URL); is = httpConn.openInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int ch = 0; while ((ch = is.read()) != -1) { baos.write(ch); } byte[] imageData = baos.toByteArray(); Image image = Image.createImage(imageData, 0, imageData.length); midlet.setImage(image); baos.close(); is.close(); httpConn.close(); } catch (IOException e) { e.printStackTrace(); } } } } |