以下载一张小图片为例子,一个较为简单的HTTP连接的demo。
支持CMNET及CMWAP两种不同的接入方式,以及在CMWAP接入方式下,过滤移动资费页面。
在6120c上测试通过。
注:若接入点选择错误,可能导致过长时间的等待
- import java.io.IOException;
- import java.io.InputStream;
- import javax.microedition.io.Connector;
- import javax.microedition.io.HttpConnection;
- 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.StringItem;
- import javax.microedition.midlet.MIDlet;
- /**
- * HttpDemo
- *
- * @author kf156(亚日)
- *
- */
- public class HttpTest extends MIDlet implements CommandListener, Runnable {
- public static final byte WAIT = 0;// 等待
- public static final byte CONNECT = 1;// 连接中
- public static final byte SUCCESS = 2;// 成功
- public static final byte FAIL = 3;// 失败
- int state;// 当前状态
- Display display = Display.getDisplay(this);
- Form form = new Form("HttpTest");
- boolean cmnet = true;// 接入点为cmnet还是cmwap
- StringBuffer sb = new StringBuffer("当前接入方式为:CMNET/n");
- StringItem si = new StringItem(null, sb.toString());
- Command connect = new Command("联网", Command.OK, 1);
- Command change = new Command("改变接入点方式", Command.OK, 2);
- Command exit = new Command("退出", Command.EXIT, 1);
- HttpConnection http;
- String host = "wiki.huihoo.com";
- String path = "/images/9/9c/Java.gif";
- public HttpTest() {
- state = WAIT;// 等待状态
- form.append(si);
- form.addCommand(connect);
- form.addCommand(change);
- form.addCommand(exit);
- form.setCommandListener(this);
- }
- protected void destroyApp(boolean b) {
- }
- protected void pauseApp() {
- }
- protected void startApp() {
- display.setCurrent(form);
- }
- public void commandAction(Command c, Displayable d) {
- if (c == change) {// 改变接入点
- if (isConnect())
- return;
- cmnet = !cmnet;
- form.deleteAll();
- sb.delete(0, sb.length());
- addStr("当前接入方式为:" + (cmnet ? "CMNET" : "CMWAP"));
- form.append(si);
- } else if (c == connect) {// 联网
- if (isConnect())
- return;
- new Thread(this).start();
- } else if (c == exit) {// 退出
- destroyApp(true);
- notifyDestroyed();
- }
- }
- public void run() {
- form.deleteAll();
- sb.delete(0, sb.length());
- addStr("当前接入方式为:" + (cmnet ? "CMNET" : "CMWAP"));
- form.append(si);
- state = CONNECT;
- addStr("网络连接中...");
- InputStream is = null;
- try {
- String url = null;
- url = cmnet ? ("http://" + host + path)
- : ("http://10.0.0.172:80" + path);
- http = (HttpConnection) Connector.open(url, Connector.READ_WRITE,
- true);
- if (!cmnet)
- http.setRequestProperty("X-Online-Host", host);
- http.setRequestMethod(HttpConnection.GET);
- String contentType = http.getHeaderField("Content-Type");
- System.out.println(contentType);
- addStr(contentType);
- if (contentType != null
- && contentType.indexOf("text/vnd.wap.wml") != -1) {// 过滤移动资费页面
- addStr("移动资费页面,过滤!");
- http.close();
- http = null;
- http = (HttpConnection) Connector.open(url,
- Connector.READ_WRITE, true);
- if (!cmnet)
- http.setRequestProperty("X-Online-Host", host);
- http.setRequestMethod(HttpConnection.GET);
- contentType = http.getHeaderField("Content-Type");
- }
- addStr("Content-Type=" + contentType);
- int code = http.getResponseCode();
- addStr("HTTP Code :" + code);
- if (code == 200) {
- addStr("网络联网成功,接收数据...");
- is = http.openInputStream();
- Image image = Image.createImage(is);
- addStr("数据接收完毕,显示图片");
- form.append(image);
- // 普通字节数据接收
- // byte[] b = new byte[1024];
- // int len = 0;
- // ByteArrayOutputStream baos = new ByteArrayOutputStream();
- // DataOutputStream dos = new DataOutputStream(baos);
- // while ((len = is.read(b)) != -1) {
- // dos.write(b, 0, len);
- // }
- // dos.close();
- // dos = null;
- // is.close();
- // is = null;
- state = SUCCESS;
- return;
- } else {
- addStr("访问页面失败");
- }
- } catch (IOException e) {
- addStr("联网发生异常:" + e.toString());
- e.printStackTrace();
- } catch (Exception e) {
- addStr("发生异常:" + e.toString());
- e.printStackTrace();
- } finally {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- is = null;
- }
- if (http != null)
- try {
- http.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- http = null;
- }
- state = FAIL;
- }
- /**
- * 判断是否正在连接
- *
- * @return
- */
- private boolean isConnect() {
- if (state == CONNECT) {
- addStr("网络连接中,请稍候...");
- return true;
- }
- return false;
- }
- /**
- * 添加文字
- *
- * @param str
- * 要添加的文字
- */
- private void addStr(String str) {
- sb.append(str + "/n");
- si.setText(sb.toString());
- }
- }
浙公网安备 33010602011771号