新聞中心

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

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

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

        為在通過游戲時顯示最高分,要與服務器進行通信,由此獲得最高分。這里,可以用我們介紹的HttpConnection,利用GET取得最高分。可以在游戲結束時使用以下方法。

        /**

        *與服務器進行通信,獲取最高分。

        */

        publicString[]getHighScore(){

        String[]str=newString[5];

        HttpConnectioncon=null;

        DataInputStreamin=null;

        try{

        con=(HttpConnection)Connector.open(SERVER_URL);

        //接收response

        in=con.openDataInputStream();

        intinput;

        inti=0;

        Strings=;

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

        if((char)input=='n'){

        str[i]=s;

        i++;

        s=;

        continue;

        }

        s=s+(char)input;

        }

        }catch(IOExceptione){

        e.printStackTrace();

        }finally{

        if(con!=null){

        try{

        con.close();

        }catch(IOExceptione1){

        e1.printStackTrace();

        }

        }

        if(in!=null){

        try{

        in.close();

        }catch(IOExceptione1){

        e1.printStackTrace();

        }

        }

        }

        returnstr;

        }

        ex.12

        下面是進行游戲時的操作。結束游戲時向服務器發(fā)送結束時間,即利用POST如下所示發(fā)送結束時間。然后,接收來自服務器的response最高分。可以在游戲結束時使用以下方法。

        /**

        *向服務器發(fā)送時間表,取得最高分

        */

        publicString[]sendScore(){

        String[]str=newString[5];

        HttpConnectioncon=null;

        DataOutputStreamout=null;

        DataInputStreamin=null;

        try{

        con=(HttpConnection)Connector.open(SERVER_URL);

        con.setRequestMethod(HttpConnection.POST);

        out=con.openDataOutputStream();

        //向服務器發(fā)送時間表

        Stringmessage=score=+second;

        byte[]messageByte=message.getBytes();

        for(inti=0;i

        out.writeByte(messageByte[i]);

        }

        out.close();

        //接收response

        in=con.openDataInputStream();

        intinput;

        inti=0;

        Strings=;

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

        if((char)input=='n'){

        str[i]=s;

        i++;

        s=;

        continue;

        }

        s=s+(char)input;

        }

        }catch(IOExceptione){

        e.printStackTrace();

        }finally{

        if(con!=null){

        try{

        con.close();

        }catch(IOExceptione1){

        e1.printStackTrace();

        }

        }

        if(out!=null){

        try{

        out.close();

        }catch(IOExceptione1){

        e1.printStackTrace();

        }

        }

        if(in!=null){

        try{

        in.close();

        }catch(IOExceptione1){

        e1.printStackTrace();

        }

        }

        }

        returnstr;

        }

        ex.13

        2.3.顯示最高分

        通過游戲和游戲結束時,都顯示最高分。用以下方法顯示最高分:

        /**

        *顯示最高分

        */

        publicvoidpaintHighScore(Graphicsg){

        for(inti=0;i

        if(highscore[i]==null)break;

        g.drawString(

        highscore[i],

        10,

        10+i*15,

        Graphics.LEFT|Graphics.TOP);

        }

        }

        ex.14

        2.4.運行

        完成的sourcecode如下:

        •BlockApplication.java

        •BlockCanvas.java

        另外,服務器使用的SERVLET的sourcecode式如下:

        •nec_server.zip

        運行后的結果如下:

        3.總結

        本講中,介紹了可以利用HTTP通信進行網(wǎng)絡編程。利用本講介紹的東西,能夠制作簡單的chat程序以及作戰(zhàn)游戲等。請大家也試著制作一些新的獨特的吧。


        上一頁 1 2 3 4 下一頁

        評論


        相關推薦

        技術專區(qū)

        關閉
        主站蜘蛛池模板: 永嘉县| 武平县| 丰宁| 密山市| 乐亭县| 清远市| 六盘水市| 武宁县| 富民县| 沧州市| 桃源县| 万年县| 阜康市| 太仆寺旗| 闸北区| 新平| 高陵县| 山丹县| 海兴县| 苗栗市| 柳江县| 牡丹江市| 南投市| 石首市| 清涧县| 东丽区| 乌兰县| 伊金霍洛旗| 盘山县| 达拉特旗| 沅江市| 读书| 西华县| 河北省| 越西县| 乌拉特中旗| 曲麻莱县| 界首市| 剑河县| 竹北市| 集贤县|