以下载一张小图片为例子,一个较为简单的HTTP连接的demo。

支持CMNET及CMWAP两种不同的接入方式,以及在CMWAP接入方式下,过滤移动资费页面。

在6120c上测试通过。

注:若接入点选择错误,可能导致过长时间的等待

 

    1. import java.io.IOException;  
    2. import java.io.InputStream;  
    3. import javax.microedition.io.Connector;  
    4. import javax.microedition.io.HttpConnection;  
    5. import javax.microedition.lcdui.Command;  
    6. import javax.microedition.lcdui.CommandListener;  
    7. import javax.microedition.lcdui.Display;  
    8. import javax.microedition.lcdui.Displayable;  
    9. import javax.microedition.lcdui.Form;  
    10. import javax.microedition.lcdui.Image;  
    11. import javax.microedition.lcdui.StringItem;  
    12. import javax.microedition.midlet.MIDlet;  
    13.   
    14. /** 
    15.  * HttpDemo 
    16.  *  
    17.  * @author kf156(亚日) 
    18.  *  
    19.  */  
    20. public class HttpTest extends MIDlet implements CommandListener, Runnable {  
    21.     public static final byte WAIT = 0;// 等待  
    22.   
    23.     public static final byte CONNECT = 1;// 连接中  
    24.   
    25.     public static final byte SUCCESS = 2;// 成功  
    26.   
    27.     public static final byte FAIL = 3;// 失败  
    28.   
    29.     int state;// 当前状态  
    30.   
    31.     Display display = Display.getDisplay(this);  
    32.   
    33.     Form form = new Form("HttpTest");  
    34.   
    35.     boolean cmnet = true;// 接入点为cmnet还是cmwap  
    36.   
    37.     StringBuffer sb = new StringBuffer("当前接入方式为:CMNET/n");  
    38.   
    39.     StringItem si = new StringItem(null, sb.toString());  
    40.   
    41.     Command connect = new Command("联网", Command.OK, 1);  
    42.   
    43.     Command change = new Command("改变接入点方式", Command.OK, 2);  
    44.   
    45.     Command exit = new Command("退出", Command.EXIT, 1);  
    46.   
    47.     HttpConnection http;  
    48.   
    49.     String host = "wiki.huihoo.com";  
    50.   
    51.     String path = "/images/9/9c/Java.gif";  
    52.   
    53.     public HttpTest() {  
    54.         state = WAIT;// 等待状态  
    55.         form.append(si);  
    56.         form.addCommand(connect);  
    57.         form.addCommand(change);  
    58.         form.addCommand(exit);  
    59.         form.setCommandListener(this);  
    60.     }  
    61.   
    62.     protected void destroyApp(boolean b) {  
    63.   
    64.     }  
    65.   
    66.     protected void pauseApp() {  
    67.   
    68.     }  
    69.   
    70.     protected void startApp() {  
    71.         display.setCurrent(form);  
    72.     }  
    73.   
    74.     public void commandAction(Command c, Displayable d) {  
    75.         if (c == change) {// 改变接入点  
    76.             if (isConnect())  
    77.                 return;  
    78.   
    79.             cmnet = !cmnet;  
    80.             form.deleteAll();  
    81.             sb.delete(0, sb.length());  
    82.             addStr("当前接入方式为:" + (cmnet ? "CMNET" : "CMWAP"));  
    83.             form.append(si);  
    84.   
    85.         } else if (c == connect) {// 联网  
    86.             if (isConnect())  
    87.                 return;  
    88.             new Thread(this).start();  
    89.   
    90.         } else if (c == exit) {// 退出  
    91.             destroyApp(true);  
    92.             notifyDestroyed();  
    93.   
    94.         }  
    95.   
    96.     }  
    97.   
    98.     public void run() {  
    99.         form.deleteAll();  
    100.         sb.delete(0, sb.length());  
    101.         addStr("当前接入方式为:" + (cmnet ? "CMNET" : "CMWAP"));  
    102.         form.append(si);  
    103.   
    104.         state = CONNECT;  
    105.         addStr("网络连接中...");  
    106.         InputStream is = null;  
    107.         try {  
    108.             String url = null;  
    109.             url = cmnet ? ("http://" + host + path)  
    110.                     : ("http://10.0.0.172:80" + path);  
    111.             http = (HttpConnection) Connector.open(url, Connector.READ_WRITE,  
    112.                     true);  
    113.             if (!cmnet)  
    114.                 http.setRequestProperty("X-Online-Host", host);  
    115.   
    116.             http.setRequestMethod(HttpConnection.GET);  
    117.   
    118.             String contentType = http.getHeaderField("Content-Type");  
    119.             System.out.println(contentType);  
    120.             addStr(contentType);  
    121.   
    122.             if (contentType != null  
    123.                     && contentType.indexOf("text/vnd.wap.wml") != -1) {// 过滤移动资费页面  
    124.                 addStr("移动资费页面,过滤!");  
    125.                 http.close();  
    126.                 http = null;  
    127.                 http = (HttpConnection) Connector.open(url,  
    128.                         Connector.READ_WRITE, true);  
    129.                 if (!cmnet)  
    130.                     http.setRequestProperty("X-Online-Host", host);  
    131.                 http.setRequestMethod(HttpConnection.GET);  
    132.                 contentType = http.getHeaderField("Content-Type");  
    133.             }  
    134.             addStr("Content-Type=" + contentType);  
    135.   
    136.             int code = http.getResponseCode();  
    137.             addStr("HTTP Code :" + code);  
    138.   
    139.             if (code == 200) {  
    140.                 addStr("网络联网成功,接收数据...");  
    141.                 is = http.openInputStream();  
    142.   
    143.                 Image image = Image.createImage(is);  
    144.                 addStr("数据接收完毕,显示图片");  
    145.                 form.append(image);  
    146.                 // 普通字节数据接收  
    147.                 // byte[] b = new byte[1024];  
    148.                 // int len = 0;  
    149.                 // ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    150.                 // DataOutputStream dos = new DataOutputStream(baos);  
    151.                 // while ((len = is.read(b)) != -1) {  
    152.                 // dos.write(b, 0, len);  
    153.                 // }  
    154.                 // dos.close();  
    155.                 // dos = null;  
    156.                 // is.close();  
    157.                 // is = null;  
    158.                 state = SUCCESS;  
    159.                 return;  
    160.             } else {  
    161.                 addStr("访问页面失败");  
    162.             }  
    163.         } catch (IOException e) {  
    164.             addStr("联网发生异常:" + e.toString());  
    165.             e.printStackTrace();  
    166.   
    167.         } catch (Exception e) {  
    168.             addStr("发生异常:" + e.toString());  
    169.             e.printStackTrace();  
    170.   
    171.         } finally {  
    172.             if (is != null) {  
    173.                 try {  
    174.                     is.close();  
    175.                 } catch (IOException e) {  
    176.                     e.printStackTrace();  
    177.                 }  
    178.                 is = null;  
    179.             }  
    180.   
    181.             if (http != null)  
    182.                 try {  
    183.                     http.close();  
    184.                 } catch (IOException e) {  
    185.                     e.printStackTrace();  
    186.                 }  
    187.             http = null;  
    188.         }  
    189.         state = FAIL;  
    190.     }  
    191.   
    192.     /** 
    193.      * 判断是否正在连接 
    194.      *  
    195.      * @return 
    196.      */  
    197.     private boolean isConnect() {  
    198.         if (state == CONNECT) {  
    199.             addStr("网络连接中,请稍候...");  
    200.             return true;  
    201.         }  
    202.         return false;  
    203.     }  
    204.   
    205.     /** 
    206.      * 添加文字 
    207.      *  
    208.      * @param str 
    209.      *            要添加的文字 
    210.      */  
    211.     private void addStr(String str) {  
    212.         sb.append(str + "/n");  
    213.         si.setText(sb.toString());  
    214.     }  

posted on 2013-03-06 13:49  爱哎唉  阅读(542)  评论(0)    收藏  举报