ExHttp.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:3k
源码类别:

J2ME

开发平台:

Java

  1. package exframework;
  2. /**
  3.  * <p>Title: ExFramework</p>
  4.  *
  5.  * <p>Description: lizhenpeng</p>
  6.  *
  7.  * <p>Copyright: Copyright (c) 2005</p>
  8.  *
  9.  * <p>Company: LP&P</p>
  10.  *
  11.  * @author lipeng
  12.  * @version 1.0
  13.  */
  14. import javax.microedition.io.*;
  15. import java.io.*;
  16. import javax.microedition.lcdui.*;
  17. public class ExHttp implements AllAction,Runnable
  18. {
  19.   HttpConnection conn;
  20.   MainForm form;
  21.   public ExHttp()
  22.   {
  23.   }
  24.   public void allAction(MainForm form)
  25.   {
  26.     ExHttp http  = new ExHttp();
  27.     http.form = form;
  28.     Thread t = new Thread(http);
  29.     t.start();
  30.    // conn=(HttpConnection)Connector.open("http://www.sina.com.cn");
  31.   // Set the request method and headers
  32.   /*
  33.   try
  34.   {
  35.     conn.setRequestMethod(HttpConnection.POST);
  36.     conn.setRequestProperty("If-Modified-Since","31 Aug 2005 0:55:31 GMT");
  37.     conn.setRequestProperty("User-Agent",
  38.                             "Profile/MIDP-2.0 Configuration/CLDC-1.0");
  39.     conn.setRequestProperty("Content-Language","en-US");
  40.   }
  41.   catch(Exception e)
  42.   {
  43.   }
  44.     StringBuffer sb=new StringBuffer(60);
  45.     sb.append("Configuration/");
  46.     sb.append(System.getProperty("microedition.configuration"));
  47.     String prof=System.getProperty("microedition.profiles");
  48.     int i=0;
  49.     int j=0;
  50.     while((j=prof.indexOf(' ',i))!=-1)
  51.     {
  52.       sb.append(" Profile/");
  53.       sb.append(prof.substring(i,j));
  54.       i=j+i;
  55.     }
  56.     sb.append(" Profile/");
  57.     sb.append(prof.substring(i));
  58. */
  59.   //设置请求为POST
  60.   /*
  61.   //conn.setRequestMethod(HttpConnection.POST);
  62.  // os=conn.openOutputStream();
  63. //  os.write("hello".getBytes());
  64.  // os.flush();
  65.   //int status=conn.getResponseCode();//判断连接是否正常
  66.   if(status==HttpConnection.HTTP_OK)
  67.   {
  68.     is = conn.openInputStream();//打开输入流
  69.     int ch;
  70.     StringBuffer buf = new StringBuffer(100000);
  71.     while((ch=is.read())!=-1)
  72.     {
  73.       //处理数据
  74.       buf.append((char)ch);
  75.     }
  76.     System.out.println(buf);
  77.       }*/
  78.   }
  79.   public void run()
  80.   {
  81.     try
  82.     {
  83.       conn=(HttpConnection)Connector.open("http://localhost:8080/examples/images/bg.png");
  84.       InputStream in = conn.openInputStream();
  85.       ByteArrayOutputStream baos = new ByteArrayOutputStream();
  86.       int oneByte;
  87.       while((oneByte = in.read())!=-1)
  88.       {
  89.         baos.write(oneByte);
  90.       }
  91.       byte[] b = baos.toByteArray();
  92.       Image image = Image.createImage(b,0,b.length);
  93.       this.form.append(image);
  94.       if(in!=null)
  95.       {
  96.         in.close();
  97.       }
  98.       if(conn!=null)
  99.       {
  100.         conn.close();
  101.       }
  102.     }
  103.     catch(Exception e)
  104.     {
  105.       System.out.print(e);
  106.     }
  107.   }
  108. }