J2ME 中的无线通信协议

来源:互联网 发布:仙知机器人 编辑:程序博客网 时间:2024/06/10 07:37
  1. package HttpConnection;
  2. import java.io.DataInputStream;
  3. import java.io.IOException;
  4. import javax.microedition.io.ConnectionNotFoundException;
  5. import javax.microedition.io.Connector;
  6. import javax.microedition.io.HttpConnection;
  7. /**********************************************************************
  8.  * 
  9.  * @author 
  10.  *Description:发送请求消息,返回字符串数据
  11. ***********************************************************************/
  12. public class HttpStringThread implements Runnable{
  13.     String host;
  14.     String parameter;
  15.     HttpConnection hc;
  16.     DataInputStream dis;
  17.     public String result;
  18.     String canvas;
  19.     String resURL;
  20.     TaskInterface ti;
  21.     public HttpStringThread(String resURL,TaskInterface ti){
  22.         this.resURL = resURL;
  23.         this.canvas=null;
  24.         if(resURL.indexOf("/") == -1){
  25.             host = resURL;
  26.             parameter = "";
  27.         }else{
  28.             host = resURL.substring(0,resURL.indexOf("/"));
  29.             parameter = resURL.substring(resURL.indexOf("/"));
  30.         }
  31.         this.ti=ti;
  32.     }
  33.     public HttpStringThread(String resURL){
  34.         ti=null;
  35.         this.resURL = resURL;
  36.         this.canvas=null;
  37.         if(resURL.indexOf("/") == -1){
  38.             host = resURL;
  39.             parameter = "";
  40.         }else{
  41.             host = resURL.substring(0,resURL.indexOf("/"));
  42.             parameter = resURL.substring(resURL.indexOf("/"));
  43.         }
  44.     }
  45.     public HttpStringThread(String resURL,TaskInterface ti,String canvas){
  46.         this.resURL = resURL;
  47.         this.canvas=canvas;
  48.         if(resURL.indexOf("/") == -1){
  49.             host = resURL;
  50.             parameter = "";
  51.         }else{
  52.             host = resURL.substring(0,resURL.indexOf("/"));
  53.             parameter = resURL.substring(resURL.indexOf("/"));
  54.         }
  55.         this.ti=ti;
  56.     }
  57.     public void run() {
  58.         // TODO 自动生成方法存根
  59.         try {
  60.             /*
  61.             hc = (HttpConnection)Connector.open("http://" + "10.0.0.172:80"    //注,这里重点,代表某种机子通信需要代理
  62.             + parameter,3,true);                                                                              //服务地址,才能实现无线通信,
  63.             hc.setRequestProperty("X-Online-Host",host);
  64.             hc.setRequestProperty("Connection","close");
  65.             */
  66.             
  67.             
  68.                 hc = (HttpConnection)Connector.open("http://"+resURL);  //这种通信代表某和机子已经包含有代理服务地址
  69.                 hc.setRequestMethod(HttpConnection.GET);                  //设置
  70.                 dis = hc.openDataInputStream();
  71.                 int k;
  72.                 byte[] data = new byte[10240];
  73.                 int i = 0;
  74.                 while((k = dis.read()) != -1){
  75.                     data[i] = (byte)k;
  76.                     i++;
  77.                 }
  78.                 String s = new String(data,0,i);
  79.                 result=s;
  80.                 System.out.println("This is HttpStringThread");
  81.                 
  82.             }catch(NullPointerException e){
  83.                 result="CommunicationError";
  84.             }catch(ConnectionNotFoundException e){
  85.                 result="CommunicationError";
  86.             }catch(IOException e){
  87.                 result="CommunicationError";
  88.             }catch(Exception e){
  89.                 result="CommunicationError";
  90.             }finally{
  91.                 System.out.println(result);
  92.                 try {
  93.                     hc.close();
  94.                     if(dis!=null){
  95.                         dis.close();
  96.                     }
  97.                     
  98.                 } catch (IOException e) {
  99.                     // TODO 自动生成 catch 块
  100.                     e.printStackTrace();
  101.                 }
  102.                 if(this.canvas!=null){
  103.                     ti.data(result, this.canvas);
  104.                 }else{
  105.                     ti.data(result);
  106.                 }
  107.             }
  108.     }
  109. }
原创粉丝点击