ExHttp.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:3k
源码类别:
J2ME
开发平台:
Java
- package exframework;
- /**
- * <p>Title: ExFramework</p>
- *
- * <p>Description: lizhenpeng</p>
- *
- * <p>Copyright: Copyright (c) 2005</p>
- *
- * <p>Company: LP&P</p>
- *
- * @author lipeng
- * @version 1.0
- */
- import javax.microedition.io.*;
- import java.io.*;
- import javax.microedition.lcdui.*;
- public class ExHttp implements AllAction,Runnable
- {
- HttpConnection conn;
- MainForm form;
- public ExHttp()
- {
- }
- public void allAction(MainForm form)
- {
- ExHttp http = new ExHttp();
- http.form = form;
- Thread t = new Thread(http);
- t.start();
- // conn=(HttpConnection)Connector.open("http://www.sina.com.cn");
- // Set the request method and headers
- /*
- try
- {
- conn.setRequestMethod(HttpConnection.POST);
- conn.setRequestProperty("If-Modified-Since","31 Aug 2005 0:55:31 GMT");
- conn.setRequestProperty("User-Agent",
- "Profile/MIDP-2.0 Configuration/CLDC-1.0");
- conn.setRequestProperty("Content-Language","en-US");
- }
- catch(Exception e)
- {
- }
- StringBuffer sb=new StringBuffer(60);
- sb.append("Configuration/");
- sb.append(System.getProperty("microedition.configuration"));
- String prof=System.getProperty("microedition.profiles");
- int i=0;
- int j=0;
- while((j=prof.indexOf(' ',i))!=-1)
- {
- sb.append(" Profile/");
- sb.append(prof.substring(i,j));
- i=j+i;
- }
- sb.append(" Profile/");
- sb.append(prof.substring(i));
- */
- //设置请求为POST
- /*
- //conn.setRequestMethod(HttpConnection.POST);
- // os=conn.openOutputStream();
- // os.write("hello".getBytes());
- // os.flush();
- //int status=conn.getResponseCode();//判断连接是否正常
- if(status==HttpConnection.HTTP_OK)
- {
- is = conn.openInputStream();//打开输入流
- int ch;
- StringBuffer buf = new StringBuffer(100000);
- while((ch=is.read())!=-1)
- {
- //处理数据
- buf.append((char)ch);
- }
- System.out.println(buf);
- }*/
- }
- public void run()
- {
- try
- {
- conn=(HttpConnection)Connector.open("http://localhost:8080/examples/images/bg.png");
- InputStream in = conn.openInputStream();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int oneByte;
- while((oneByte = in.read())!=-1)
- {
- baos.write(oneByte);
- }
- byte[] b = baos.toByteArray();
- Image image = Image.createImage(b,0,b.length);
- this.form.append(image);
- if(in!=null)
- {
- in.close();
- }
- if(conn!=null)
- {
- conn.close();
- }
- }
- catch(Exception e)
- {
- System.out.print(e);
- }
- }
- }