exhttp.java

来自「< J2ME手机游戏开发技术详解>>上的全部源码,包括实现中需」· Java 代码 · 共 120 行

JAVA
120
字号
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);
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?