HTTP联网开发小例子

来源:互联网 发布:sql怎么建立数据库 编辑:程序博客网 时间:2024/05/01 13:25

作者:gaogao   文章来源:http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=801

众所周知,支持MIDP1.0的手机如果想使用联网应用一般都只能使用HTTP服务,无论是上传游戏的最高分,还是动态的下载地图资源,图片资源都需要使用HTTP协议进行通信。参考MIDP1.0的有关文档(包括Sun 和 Nokia)使我们了解到,一般来说可以选择Tomcat这样的服务器运行Java Servlet来作为手机与各种服务包括访问数据库、下载图片的服务中介,在格式上我们常常使用“text/plain”这样的格式,加入中文的时候可能还要加上与中文相关的字符集代号"GB2312"等等,关于服务器这些技术请参考J2EE的相关知识。使用text/plain的时候,我们其实获得了一个文档,我们可以从这个文档中读出我们需要的任何东西。

          进行联网开发的时候我们需要定义一些通信协议,最简单的例子,我们在RPG中可能需要在网上下载地图和对话字符以及图片,我们就发送一条:“GET|STRINGS|ID=5”(注意是我们http包中的内容),然后服务器返回一段字符串就完成了一次HTTP通信;"GET|PIC|ID=100",服务器返回一个图片的二进制byte[]就可以了。总之,服务器和手机的通信可以归纳成 : 二进制流对二进制流二进制流的操作。

         我们如果以单线程进行通信,一个操作要等等几秒钟,用户难免会觉得非常难以忍受,我们必须使用多线程的方式让用户能够做别的一些事情,而不是单纯的等待,就算是加入动态的现实也比单纯的通信,等待要好得多的多.多线程在我的一篇处理Loading状态的文章中有所体现,可以参照里面的思想.

代码:java serlvet....

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class HttpGameServer
    extends javax.servlet.http.HttpServlet {
  public void HttpServlet() {
  }

  public void doPost(HttpServletRequest req, HttpServletResponse resp) {
    int infoLength = req.getContentLength();
    System.out.println("req.getContentLength()" + infoLength);
    ;
    ResourceBundle rb =
        ResourceBundle.getBundle("LocalStrings", req.getLocale());
    resp.setContentType("text/plain");
    try {
      InputStream is = req.getInputStream();
      byte[] bInfoBytes = new byte[infoLength];
      // is.read(bInfoBytes);
      DataInputStream dis = new DataInputStream(is);
      System.out.println("System Running...");
   //
   对输入流的处理
   //
      PrintWriter out = resp.getWriter();
      out.write("TEST OK");
    }
    catch (Exception ex) {
      System.out.println(ex.getMessage());
    }
  }
}

j2me:

import javax.microedition.io.*;
import java.io.*;

public class Http{
  HttpConnection httpConn;
  public Http(String url)
  {
    try {
      postViaHttpConnection(url);
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  void postViaHttpConnection(String url) throws IOException {
    HttpConnection c = null;
    InputStream is = null;
    OutputStream os = null;

    try {
        c = (HttpConnection)Connector.open(url);
        c.setRequestMethod(HttpConnection.POST);
        c.setRequestProperty("If-Modified-Since",
            "29 Oct 1999 19:43:31 GMT");
        c.setRequestProperty("User-Agent",
            "Profile/MIDP-1.0 Configuration/CLDC-1.0");
        c.setRequestProperty("Content-Language", "en-US");

        // Getting the output stream may flush the headers
        os = c.openOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        os.write("HELLO,WORLD".getBytes());
        // 一些手机,在此加入flush可能导致http server不能获得请求信息。
        //os.flush();// nokia 需要加入

        is = c.openInputStream();

        String type = c.getType();
        System.out.println("type : " + type);
        DataInputStream dis = new DataInputStream(is);
        int length = dis.available();
        byte [] reponseBytes = new byte[length];
        dis.read(reponseBytes);
        System.out.println("Received . :" + new String(reponseBytes));

    } finally {
        if (is != null)
            is.close();
        if (os != null)
            os.close();
        if (c != null)
            c.close();
    }
}

}

 

 

import javax.microedition.midlet.*;

public class Main extends MIDlet {
  public Main() {
  }
  protected void pauseApp() {
    /**@todo Implement this javax.microedition.midlet.MIDlet abstract method*/
  }
  protected void startApp() throws javax.microedition.midlet.MIDletStateChangeException {
    /**@todo Implement this javax.microedition.midlet.MIDlet abstract method*/
     new Http("http://127.0.0.1:8080/examples/servlet/HttpGameServer");

  }
  protected void destroyApp(boolean parm1) throws javax.microedition.midlet.MIDletStateChangeException {
    /**@todo Implement this javax.microedition.midlet.MIDlet abstract method*/
  }

}

    综上所述,j2me与j2ee所有的通信都可以用这个小原型来继续开发,可以开发动态下载图片\地图资源等等的东西,也可以使用j2me进行数据库的管理等等高级开发应用.