📄 onlinescoring.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class OnlineScoring extends Form implements CommandListener
{
private int score;
private Command cancel;
private Command send;
private Command ok;
private HttpConnection httpConnection;
public OnlineScoring(int scoreArg)
{
super("Online Score");
score = scoreArg;
append("Transmit score of " + score + "?");
send = new Command("Send", Command.OK, 1);
addCommand(send);
cancel = new Command("Cancel", Command.CANCEL, 2);
addCommand(cancel);
setCommandListener(this);
}
public void showResult(String s)
{
append(s);
}
public void commandAction(Command c, Displayable s)
{
if (c == send)
{
// get rid of the send command from the form
removeCommand(send);
// create a new thread for the networking
Thread t = new Thread()
{
// declare a run method (not called until the thread is started
// (see t.start below)
public void run()
{
// send score to the server and retrieve the resulting rank
// WARNING: this method will STALL until we get a response
// or it times out
String result = transmitScore();
// delete the current text item
delete(0);
// present the result to the player
showResult(result);
// get rid of the cancel command and add an OK
removeCommand(cancel);
ok = new Command("OK", Command.OK, 1);
addCommand(ok);
}
};
// start the thread (execute the contents of the run method above)
t.start();
}
if (c == ok || c == cancel)
StarAssault.getApp().activateMenu();
}
public String transmitScore()
{
InputStream in = null;
OutputStream out = null;
try
{
// submit score to server and read ranking response
httpConnection = (HttpConnection) Connector.open("http://localhost:8080/StarAssault/updateScore");
// close the connection after req/response (don't bother keeping it alive)
httpConnection.setRequestProperty("Connection", "close");
// setup for a post
httpConnection.setRequestMethod(httpConnection.POST);
String req = "" + score;
httpConnection.setRequestProperty("Content-Length", Integer.toString(req.length()));
// output the request
out = httpConnection.openOutputStream();
for (int i = 0; i < req.length(); i++)
out.write(req.charAt(i));
// read the result
in = httpConnection.openInputStream();
if (httpConnection.getResponseCode() == httpConnection.HTTP_OK)
{
int contentLength = (int) httpConnection.getLength();
if (contentLength == -1) contentLength = 255;
StringBuffer response = new StringBuffer(contentLength);
for (int i = 0; i < contentLength; i++)
response.append((char) in.read());
String rankString = response.toString();
return "You are currently ranked " + rankString + ".";
}
else
throw new IOException();
}
catch (IOException e)
{
return "Error transmitting score.";
}
finally
{
if (httpConnection != null)
{
try
{
httpConnection.close();
}
catch (IOException ioe)
{
}
}
if (in != null)
{
try
{
in.close();
}
catch (IOException ioe)
{
}
}
if (out != null)
{
try
{
// clean up
out.close();
}
catch (IOException ioe)
{
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -