日期:2014-05-20  浏览次数:20867 次

J2ME无线联网技术中的HTTP连接[转]
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;

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.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
* 在运行的TextBox输入框中输入一个图片的网址,下载到模拟器中
*
* 例如:在输入框中输入:www.baidu.com/img/logo.gif
*
* @author mao
*
*/
public class ViewImage extends MIDlet implements CommandListener {

??? private Display display;
??? private TextBox tbMain;
??? private Alert alStatus;
??? private Form fm;
??? private Command cmExit;
??? private Command cmView;
??? private Command cmBack;
??? private static final int ALERT_DISPLAY_TIME = 3000;
??? Image im = null;

??? public ViewImage() {
??? ??? display = Display.getDisplay(this);
??? ??? tbMain = new TextBox("输入图片地址:", "http://", 75, 0);
??? ??? cmExit = new Command("退出", Command.EXIT, 1);
??? ??? cmView = new Command("下载", Command.SCREEN, 2);
??? ??? tbMain.addCommand(cmExit);
??? ??? tbMain.addCommand(cmView);
??? ??? tbMain.setCommandListener(this);
??? ??? fm = new Form("");
??? ??? cmBack = new Command("后退", Command.BACK, 1);
??? ??? fm.addCommand(cmBack);
??? ??? fm.setCommandListener(this);
??? }

??? protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
??? }

??? protected void pauseApp() {
??? }

??? protected void startApp() throws MIDletStateChangeException {
??? ??? display.setCurrent(tbMain);
??? }

??? public void commandAction(Command c, Displayable s) {
??? ??? if (c == cmExit) {
??? ??? ??? try {
??? ??? ??? ??? destroyApp(false);
??? ??? ??? ??? notifyDestroyed();
??? ??? ??? } catch (MIDletStateChangeException e) {
??? ??? ??? ??? e.printStackTrace();
??? ??? ??? }
??? ??? } else if (c == cmView) {
??? ??? ??? showAlert("下载中...", false, tbMain);
??? ??? ??? Download dl = new Download(tbMain.getString(), this);
??? ??? ??? dl.start();
??? ??? } else if (c == cmBack) {
??? ??? ??? display.setCurrent(tbMain);
??? ??? }
??? }

??? public void showImage(boolean flag) {
??? ??? if (flag == false)
??? ??? ??? showAlert("下载失败...", true, tbMain);
??? ??? else {
??? ??? ??? ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT,
??? ??? ??? ??? ??? null);
??? ??? ??? if (fm.size() != 0)
??? ??? ??? ??? fm.set(0, ii);
??? ??? ??? else
??? ??? ??? ??? fm.append(ii);
??? ??? ??? showAlert("下载成功", true, fm);
??? ??? }
??? }

??? public void showAlert(String msg, boolean modal, Displayable displayable) {
??? ??? alStatus = new Alert("状态", msg, null, AlertType.INFO);
??? ??? if (modal)
??? ??? ??? alStatus.setTimeout(Alert.FOREVER);
??? ??? else
??? ??? ??? alStatus.setTimeout(ALERT_DISPLAY_TIME);
??? ??? display.setCurrent(alStatus, displayable);
??? }
}

class Download implements Runnable {

??? private String url;
??? private ViewImage Midlet;
??? private boolean downloadSuccess = false;

??? public Download(String url, ViewImage Midlet) {
??? ??? this.url = url;
??? ??? this.Midlet = Midlet;
??? }

??? public void run() {
??? ??? try {
??? ??? ??? getImage(url);
??? ??? } catch (Exception e) {
??? ??? ??? System.err.println("Msg : " + e.toString());
??? ??? }
??? }

??? public void start() {
??? ??? Thread thread = new Thread(this);
??? ??? try {
??? ??? ??? thread.start();
??? ??? } catch (Exception e) {
??? ??? }
??? }

??? private void getImage(String url) throws IOException {

??? ??? System.out.println();
??? ???
??? ??? System.out.println("this is the url : " + url);

??? ??? ContentConnection connection = (ContentConnection) Connector.open(url);

??? ??? System.out.println("connection : " + connection);

??? ??? DataInputStream iStrm = connection.openDataInputStream();
??? ???
??? ??? System.out.println("iStrm: " + iStrm);
??? ???
??? ??? ByteArrayOutputStream bStrm = null;
??? ??? Image im = null;
??? ??? try {
??? ??? ??? byte imageData[];
??? ??? ??? int length = (int) connection.getLength();
??? ??? ??? if (length != -1) {
??? ??? ??? ??? imageData = new byte[length];
??? ??? ??? ??? iStrm.readFully(imageData);
??? ??? ??? } else {
??? ??? ??? ??? bStrm = new ByteArrayOutputStream();
??? ??? ??? ??? int ch;
??? ??? ??? ??? while ((ch = iStrm.read()) != -1)
??? ??? ??? ??? ??? bStrm.write(ch);
??? ??? ??? ??? imageData = bStrm.toByteArray();
??? ??? ??? }
??? ??? ??? im = Image.createImage(imageData , 0 , imageData.length);
??? ??? } finally {
??? ??? ??? if (connection != null)
??? ??? ??? ??? connection.close();
??? ??? ??? if (iStrm != null)
??? ??? ??? ??? iStrm.close();
??? ??? ??? if (bStrm != null)
??? ??? ??? ??? bStrm.close();
??? ??? }
??? ??? if (im == null)
??? ??? ??? Midlet.showImage(false);
??? ??? else {
??? ??? ??? Midlet.im = im;
??? ??? ??? Midlet.showImage(true);
??? ??? }
??? }
}

?

?

?

http://www.java-cn.com/club/html/63/n-163-3.htmlj2me最佳联网方案终结版
用 J2ME 进行联网用 J2ME 进行联网http://www.ibm.com/developerworks/cn/java/wi-jio/

?