進行聯網開發的時候我們需要定義一些通信協議,最簡單的例子,我們在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進行數據庫的管理等等高級開發應用.