|
|
作者:未知 文章来源:nec ♦ 引言
/**
* 处理post request
*/
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//接收参数
String message = req.getParameter("message");
//转发文字
String reverse = "";
for(int i = message.length();i > 0;i--){
reverse = reverse + message.charAt(i-1);
}
//写response
PrintWriter out = res.getWriter();
out.write("<html>\n");
out.write("<head>\n");
out.write("<title>Request is POST.</title>\n");
out.write("</head>\n");
out.write("<body>\n");
out.write("message is "+message+"<br>\n");
out.write("Reverse Message is "+reverse+"\n");
out.write("</body>\n");
out.write("</html>");
out.close();
}
}
ex. 9
实际操作如下所示:
2. 制作应用程序
现在,我们介绍如何实际制作利用HTTP通信的应用程序。这次增添了以前所作的泡泡龙游戏(BlockApplication)的内容,并把游戏结束的时间作成高低分一览表,由服务器管理。为了使程序更简单,还省略了与声音相关的操作。以下是内容改变前的source code:
&#8226; BlockApplication.java
&#8226; BlockCanvas.java
给这个程序添加
&#8226; 计算结束时间功能。
&#8226; 向服务器发送时间表功能。
&#8226; 从服务器接收最高分功能。
通过游戏或游戏结束时,显示出最高分。
为了计算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 = 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);
}
out.close();
//接收response
in = con.openDataInputStream();
int input;
int i = 0;
String s = "";
while ((input = in.read()) != -1) {
if ((char) input == '\n') {
str = 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 == null)break;
g.drawString(
highscore,
10,
10 + i * 15,
Graphics.LEFT | Graphics.TOP);
}
}
ex. 14
2.4. 运行
完成的source code如下:
&#8226; BlockApplication.java
&#8226; BlockCanvas.java
另外,服务器使用的SERVLET的source code 式如下:
&#8226; nec_server.zip
运行后的结果如下:
3. 总结
在本讲中,介绍了可以利用HTTP通信进行网络编程。利用本讲介绍的东西,能够制作简单的chat程序以及联网作战游戏等应用程序。请大家也试着制作一些新的独特的应用程序吧。
|
|