📄 autoupload.java
字号:
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.rms.RecordStore;
public class AutoUpload implements Runnable {
private GalaxyWarCanvas sampleGame;
private RecordStore rs;
// Please change the following parameters accordingly
String IP = "mgpp.mymobilesoft.com";
String PORT = "80";
String PATH = "/mgpp/UploadScore";
// Please change the following parameters accordingly
String gameid = "040414150053129";
String userid = "default";
String mobile = "8613800000000";
String prefix = "http://" + IP + ":" + PORT + PATH;
String info = readUploadSetting();
public AutoUpload(GalaxyWarCanvas sampleGame) {
this.sampleGame = sampleGame;
}
void start() {
Thread t = new Thread(this);
try {
t.start();
} catch (Exception e) {}
}
public void run() {
try {
String requestBody = formatRequestBody();
submitScore(prefix, requestBody);
} catch (IOException e) {
sampleGame.isReaded = true;
System.out.println("autoUpload:" + e);
}
sampleGame.isUploaded = true;
//sampleGame.isReaded = true;
}
private String formatRequestBody() {
if (sampleGame.userID.length() != 0
&& sampleGame.mobile.length() != 0) {
userid = sampleGame.userID;
mobile = sampleGame.mobile;
}else
if (info.length() > 1) {
userid = info.substring(0, info.indexOf(","));
mobile = info.substring(info.indexOf(",") + 1, info.length());
}
StringBuffer b = new StringBuffer();
b.append("gameid=");
//b.append(gameidField.getString());
b.append(gameid);
b.append("&userid=");
b.append(userid);
b.append("&mobile=");
b.append(mobile);
//b.append("&level=");
//b.append(levelField.getString());
//b.append(sampleGame.uploadLevel);
b.append("&score=");
//b.append(scoreField.getString());
b.append(sampleGame.uploadScore);
b.append("&time=0");
System.out.println(b.toString());
return b.toString();
}
void submitScore(String urlString
, String requestBodyString) throws IOException {
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
int rc;
try {
// Establish HTTP connection to game portal
c = (HttpConnection) Connector.open(urlString, Connector.READ_WRITE);
// Set the request method to POST
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("User-Agent"
, "Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Type"
, "application/x-www-form-urlencoded");
// Obtain output stream for sending the request string
os = c.openOutputStream();
os.write(requestBodyString.getBytes());
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
//Obtain input stream for receiving server response
is = c.openDataInputStream();
//Retrieve the response from the server
int ch;
StringBuffer b = new StringBuffer();
// Get the Content-length header
String lengthStr = c.getHeaderField("Content-length");
int lengthInt = Integer.parseInt(lengthStr);
// Get response data
for (int i = 0; i < lengthInt; i++) {
ch = is.read();
b.append((char) ch);
}
} catch (IOException e) {
sampleGame.isReaded = true;
System.out.println("auto upload: " + e);
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (c != null) {
c.close();
}
}
}
private String readUploadSetting() {
byte[] rec;
String result = "";
try {
rs = RecordStore.openRecordStore("galaxywar", false);
rec = new byte[300];
rs.getRecord(3, rec, 0);
rs.closeRecordStore();
if (rec[0] != 0)
result = new String(rec);
} catch (Exception ex) {
System.out.println("readScore: " + ex);
}
return result.trim();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -