眾所周知,支持MIDP1.0的手機如果想使用聯網應用一般都只能使用HTTP服務,無論是上傳游戲的最高分,還是動態的下載地圖資源,圖片資源都需要使用HTTP協議進行通信。參考MIDP1.0的有關文檔(包括Sun 和 Nokia)使我們了解到,一般來說可以選擇Tomcat這樣的服務器運行java Servlet來作為手機與各種服務包括訪問數據庫、下載圖片的服務中介,在格式上我們常常使用“text/plain”這樣的格式,加入中文的時候可能還要加上與中文相關的字符集代號"GB2312"等等,關于服務器這些技術請參考J2EE的相關知識。使用text/plain的時候,我們其實獲得了一個文檔,我們可以從這個文檔中讀出我們需要的任何東西。
進行聯網開發的時候我們需要定義一些通信協議,最簡單的例子,我們在RPG中可能需要在網上下載地圖和對話字符以及圖片,我們就發送一條:“GETSTRINGSID=5”(注意是我們http包中的內容),然后服務器返回一段字符串就完成了一次HTTP通信;"GETPICID=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. ;
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進行數據庫的管理等等高級開發應用.
(出處:http://www.49028c.com)
新聞熱點
疑難解答