国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

HTTP聯(lián)網(wǎng)開發(fā)小例子

2019-11-18 16:11:47
字體:
供稿:網(wǎng)友

          眾所周知,支持MIDP1.0的手機(jī)如果想使用聯(lián)網(wǎng)應(yīng)用一般都只能使用HTTP服務(wù),無論是上傳游戲的最高分,還是動態(tài)的下載地圖資源,圖片資源都需要使用HTTP協(xié)議進(jìn)行通信。參考MIDP1.0的有關(guān)文檔(包括Sun 和 Nokia)使我們了解到,一般來說可以選擇Tomcat這樣的服務(wù)器運(yùn)行java Servlet來作為手機(jī)與各種服務(wù)包括訪問數(shù)據(jù)庫、下載圖片的服務(wù)中介,在格式上我們常常使用“text/plain”這樣的格式,加入中文的時(shí)候可能還要加上與中文相關(guān)的字符集代號"GB2312"等等,關(guān)于服務(wù)器這些技術(shù)請參考J2EE的相關(guān)知識。使用text/plain的時(shí)候,我們其實(shí)獲得了一個文檔,我們可以從這個文檔中讀出我們需要的任何東西。

          進(jìn)行聯(lián)網(wǎng)開發(fā)的時(shí)候我們需要定義一些通信協(xié)議,最簡單的例子,我們在RPG中可能需要在網(wǎng)上下載地圖和對話字符以及圖片,我們就發(fā)送一條:“GETSTRINGSID=5”(注意是我們http包中的內(nèi)容),然后服務(wù)器返回一段字符串就完成了一次HTTP通信;"GETPICID=100",服務(wù)器返回一個圖片的二進(jìn)制byte[]就可以了。總之,服務(wù)器和手機(jī)的通信可以歸納成 : 二進(jìn)制流對二進(jìn)制流二進(jìn)制流的操作。

         我們?nèi)绻詥尉€程進(jìn)行通信,一個操作要等等幾秒鐘,用戶難免會覺得非常難以忍受,我們必須使用多線程的方式讓用戶能夠做別的一些事情,而不是單純的等待,就算是加入動態(tài)的現(xiàn)實(shí)也比單純的通信,等待要好得多的多.多線程在我的一篇處理Loading狀態(tài)的文章中有所體現(xiàn),可以參照里面的思想.

代碼: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());
        // 一些手機(jī),在此加入flush可能導(dǎo)致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所有的通信都可以用這個小原型來繼續(xù)開發(fā),可以開發(fā)動態(tài)下載圖片\地圖資源等等的東西,也可以使用j2me進(jìn)行數(shù)據(jù)庫的管理等等高級開發(fā)應(yīng)用.

(出處:http://m.survivalescaperooms.com)



發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 邵武市| 桦南县| 乌海市| 印江| 灵宝市| 新龙县| 昭觉县| 自治县| 大渡口区| 石嘴山市| 高唐县| 扶余县| 抚远县| 双鸭山市| 通道| 深圳市| 大安市| 阳新县| 锡林浩特市| 北辰区| 金川县| 丰原市| 新昌县| 滦平县| 外汇| 博湖县| 商河县| 全州县| 贞丰县| 封丘县| 宜昌市| 宜黄县| 专栏| 万载县| 霸州市| 泉州市| 湘阴县| 津南区| 育儿| 蓬溪县| 嵩明县|