2. 制作应用程序
   现在,我们介绍如何实际制作利用HTTP通信的应用程序。这次增添了以前所作的泡泡龙游戏(BlockApplication)的内容,并把游戏结束的时间作成高低分一览表,由服务器管理。为了使程序更简单,还省略了与声音相关的操作。以下是内容改变前的source code:
• BlockApplication.java
• BlockCanvas.java
   给这个程序添加
• 计算结束时间功能。

• 向服务器发送时间表功能。

• 从服务器接收最高分功能。

通过游戏或游戏结束时,显示出最高分。

   为了计算Http通信和时间表,给实际变量添加以下变量:

//相关经过时间
private int second = 0;
private long startMs;

//http通信类
private final String SERVER_URL =
"http://localhost:8080/nec_server/servlet/BlockScoreServlet";//服务器的UPL
private String[] highscore = null;//最高分

ex. 10

     2.1. 计算结束时间

     为了计算结束时间,要记录游戏开始时的系统时间(startMs),以这个时间为准计算经过时间,如下画面所示:

/*****************************************
* 计时器相关处理
*****************************************/
/**
* 计算经过时间
*/
public int getSecond() {
   return (int)((System.currentTimeMillis()-startMs)/1000);
}
ex. 11

     2.2. 向服务器发送时间表,接收最高分

   如上所述,游戏结束时显示最高分。但是,游戏结束时向服务器发送时间表,而通过游戏时不发送时间表,只显示最高分。

   为在通过游戏时显示最高分,要与服务器进行通信,由此获得最高分。这里,可以用我们介绍的HttpConnection,利用GET取得最高分。可以在游戏结束时使用以下方法。

/**
* 与服务器进行通信,获取最高分。
*/
public String[] getHighScore() {
   String[] str = new String[5];
   HttpConnection con = null;
   DataInputStream in = null;
   try {
     con = (HttpConnection) Connector.open(SERVER_URL);
//接收response
     in = con.openDataInputStream();
     int input;
     int i = 0;
     String s = "";
     while ((input = in.read()) != -1) {
       if ((char) input == '\n') {
         str[i] = s;
         i++;
         s = "";
         continue;
       }
       s = s + (char) input;
     }

   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     if (con != null) {
     try {
       con.close();
     } catch (IOException e1) {
       e1.printStackTrace();
     }
   }
     if (in != null) {
       try {
         in.close();
       } catch (IOException e1) {
         e1.printStackTrace();
       }
     }
   }

   return str;
}

ex. 12

     下面是进行游戏时的操作。结束游戏时向服务器发送结束时间,即利用POST如下所示发送结束时间。然后,接收来自服务器的response最高分。可以在游戏结束时使用以下方法。

/**
* 向服务器发送时间表,取得最高分
*/
public String[] sendScore() {
   String[] str = new String[5];
   HttpConnection con = null;
   DataOutputStream out = null;
   DataInputStream in = null;
   try {
     con = (HttpConnection) Connector.open(SERVER_URL);
     con.setRequestMethod(HttpConnection.POST);

     out = con.openDataOutputStream();

     //向服务器发送时间表
     String message = "score=" + second;
     byte[] messageByte = message.getBytes();
     for (int i = 0; i < messageByte.length; i++) {
       out.writeByte(messageByte[i]);
     }
     out.close();

     //接收response
     in = con.openDataInputStream();
     int input;
     int i = 0;
     String s = "";
     while ((input = in.read()) != -1) {
       if ((char) input == '\n') {
         str[i] = s;
         i++;
         s = "";
         continue;
       }
       s = s + (char) input;
     }

   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     if (con != null) {
       try {
         con.close();
       } catch (IOException e1) {
         e1.printStackTrace();
       }
     }
     if (out != null) {
       try {
         out.close();
       } catch (IOException e1) {
         e1.printStackTrace();
       }
     }

     if (in != null) {
       try {
         in.close();
       } catch (IOException e1) {
         e1.printStackTrace();
       }
     }
   }

   return str;
}

ex. 13

   2.3. 显示最高分

   通过游戏和游戏结束时,都显示最高分。用以下方法显示最高分:

/**
* 显示最高分
*/
public void paintHighScore(Graphics g){
   for (int i = 0; i < highscore.length; i++) {
     if(highscore[i] == null)break;
     g.drawString(
     highscore[i],
     10,
     10 + i * 15,
     Graphics.LEFT | Graphics.TOP);
   }
}
ex. 14

   2.4. 运行

   完成的source code如下:

• BlockApplication.java

• BlockCanvas.java

另外,服务器使用的SERVLET的source code 式如下:

• nec_server.zip

运行后的结果如下: