新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 開發(fā)MIDP聯(lián)網(wǎng)應(yīng)用程序

        開發(fā)MIDP聯(lián)網(wǎng)應(yīng)用程序

        作者: 時(shí)間:2012-05-07 來源:網(wǎng)絡(luò) 收藏

        *從控制臺(tái)輸出response

        */

        publicclassPOSTTestextendsMIDlet{

        /**

        *利用POST送信息

        */

        protectedvoidstartApp()throwsMIDletStateChangeException{

        try{

        HttpConnectioncon=(HttpConnection)Connector.open(http://localhost:8080/nec_server/servlet/ReverseServlet);

        //指定POST

        con.setRequestMethod(HttpConnection.POST);

        //在request中輸入數(shù)據(jù)

        Stringmessage=message=helloworld;

        DataOutputStreamdos=con.openDataOutputStream();

        byte[]messageByte=message.getBytes();

        for(inti=0;i

        dos.writeByte(messageByte[i]);

        }

        dos.flush();

        dos.close();

        //接收response

        DataInputStreamin=con.openDataInputStream();

        intinput;

        while((input=in.read())!=-1){

        System.out.print((char)input);

        }

        in.close();

        //關(guān)閉鏈接

        con.close();

        }catch(IOExceptione){

        e.printStackTrace();

        }

        }

        protectedvoidpauseApp(){

        }

        protectedvoiddestroyApp(booleanarg0)throwsMIDletStateChangeException{

        }

        }

        ex.8

        接下來介紹本次使用的SERVLETsample,作為服務(wù)器實(shí)際安裝的例子。因?yàn)楸局v座是講座,所以關(guān)于SERVLET的詳細(xì)說明就不再贅述,網(wǎng)上有許多這方面的信息,請大家參考Sun—http://java.sun.com/products/servlet/等網(wǎng)站。

        importjava.io.IOException;

        importjava.io.PrintWriter;

        importjavax.servlet.ServletException;

        importjavax.servlet.http.HttpServlet;

        importjavax.servlet.http.HttpServletRequest;

        importjavax.servlet.http.HttpServletResponse;

        /**

        *把接收的文字列進(jìn)行轉(zhuǎn)發(fā)的SERVLET

        */

        publicclassReverseServletextendsHttpServlet{

        /**

        *處理postrequest

        */

        protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseres)

        throwsServletException,IOException{

        //接收參數(shù)

        Stringmessage=req.getParameter(message);

        //轉(zhuǎn)發(fā)文字

        Stringreverse=;

        for(inti=message.length();i>0;i--){

        reverse=reverse+message.charAt(i-1);

        }

        //寫response

        PrintWriterout=res.getWriter();

        out.write(

        n);

        out.write(

        n);

        out.write(

        n);

        out.write(n);

        out.write(

        n);

        out.write(messageis+message+
        n);

        out.write(ReverseMessageis+reverse+n);

        out.write(

        n);

        out.write();

        out.close();

        }

        }

        ex.9

        實(shí)際操作如下所示:

        2

        2.制作

        現(xiàn)在,我們介紹如何實(shí)際制作利用HTTP通信的。這次增添了以前所作的泡泡龍游戲(BlockApplication)的內(nèi)容,并把游戲結(jié)束的時(shí)間作成高低分一覽表,由服務(wù)器管理。為了使程序更簡單,還省略了與聲音相關(guān)的操作。以下是內(nèi)容改變前的sourcecode:

        BlockApplication.java

        BlockCanvas.java

        給這個(gè)程序添加

        •計(jì)算結(jié)束時(shí)間功能。

        •向服務(wù)器發(fā)送時(shí)間表功能。

        •從服務(wù)器接收最高分功能。

        通過游戲或游戲結(jié)束時(shí),顯示出最高分。

        為了計(jì)算Http通信和時(shí)間表,給實(shí)際變量添加以下變量:

        //相關(guān)經(jīng)過時(shí)間

        privateintsecond=0;

        privatelongstartMs;

        //http通信類

        privatefinalStringSERVER_URL=

        http://localhost:8080/nec_server/servlet/BlockScoreServlet;//服務(wù)器的UPL

        privateString[]highscore=null;//最高分

        ex.10

        2.1.計(jì)算結(jié)束時(shí)間

        為了計(jì)算結(jié)束時(shí)間,要記錄游戲開始時(shí)的系統(tǒng)時(shí)間(startMs),以這個(gè)時(shí)間為準(zhǔn)計(jì)算經(jīng)過時(shí)間,如下畫面所示:

        /*****************************************

        *計(jì)時(shí)器相關(guān)處理

        *****************************************/

        /**

        *計(jì)算經(jīng)過時(shí)間

        */

        publicintgetSecond(){

        return(int)((System.currentTimeMillis()-startMs)/1000);

        }

        ex.11

        2.2.向服務(wù)器發(fā)送時(shí)間表,接收最高分

        如上所述,游戲結(jié)束時(shí)顯示最高分。但是,游戲結(jié)束時(shí)向服務(wù)器發(fā)送時(shí)間表,而通過游戲時(shí)不發(fā)送時(shí)間表,只顯示最高分。



        評論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 太湖县| 鸡东县| 甘洛县| 台北市| 府谷县| 大安市| 栖霞市| 大石桥市| 沙湾县| 烟台市| 垣曲县| 志丹县| 始兴县| 铁岭市| 八宿县| 屏南县| 射洪县| 五寨县| 阳新县| 银川市| 庆云县| 邓州市| 东方市| 罗平县| 平凉市| 和顺县| 中超| 安康市| 宜君县| 新平| 福贡县| 都匀市| 邓州市| 富阳市| 武川县| 左贡县| 连江县| 永胜县| 穆棱市| 广宁县| 屯门区|