📄 httpscoretransmitter.java
字号:
nDataEndIndex);
// Authentication help text
nDataStartIndex = m_strFullString.indexOf("infHelp=");
nDataEndIndex = m_strFullString.indexOf(m_strDelimiter, nDataStartIndex);
m_strReceivedDataAuthenticationHelp =
m_strFullString.substring(nDataStartIndex+8,
nDataEndIndex);
/*
// Statements for testing - begin
if (m_strReceivedDataHighestRank != null) {
System.out.println("Highest rank achieved = " + m_strReceivedDataHighestRank);
} else {
System.out.println("No value returned for \"Highest rank achieved\"");
}
if (m_strReceivedDataHighestRank != null) {
System.out.println("Difficulty for highest rank = " + m_strReceivedDataDifficultySetting);
} else {
System.out.println("No value returned for \"Difficulty for highest rank\"");
}
if (m_strReceivedDataHighestRank != null) {
System.out.println("Serial number = " + m_objErixMIDlet.m_strSerialNumber);
} else {
System.out.println("No value returned for \"Serial number\"");
}
System.out.println("Authentication prompt = " + m_strReceivedDataAuthenticationPrompt);
System.out.println("Authentication help = " + m_strReceivedDataAuthenticationHelp);
// Statements for testing - end.
*/
} // End of if any content has been returned in the server's response.
} // end of try block
catch (Exception objExc) {
System.out.println("Exception" + objExc);
}
} // end of method getServerresponse
/**
* This method sends the high scores to the High Scores server.
* This method uses HTTP to send data to the server.
* This method internally calls the methods for generating the authentication key
* and building the complete string to be sent over HTTP to the high score server.
@return Returns if the function was completed successfully or not.
*/
public int sendScores() {
DataOutputStream objDataOutputStream = null;
InputStream objInputStream = null;
int nHTTPResponseCode = 0;
try {
try {
// System.out.println("HST: before Connector.open");
// Open HTTP connection at URL
// m_objConnection = (HttpConnection)Connector.open(
// m_strHighScoreServerUrl, Connector.READ_WRITE, true);
m_objConnection = (HttpConnection)Connector.open(
m_strHighScoreServerUrl);
// System.out.println("HST: after Connector.open");
// Set the method for URL request
m_objConnection.setRequestMethod(HttpConnection.POST);
//Setting the header values
// m_objConnection.setRequestProperty("If-Modified-Since",
// "29 Oct 1999 19:43:31 GMT");
m_objConnection.setRequestProperty("Content-Type", "text/plain");
m_objConnection.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0");
m_objConnection.setRequestProperty("Content-Language", "en-US");
// Obtain a DataOutputStream from the HttpConnection
objDataOutputStream = new DataOutputStream(m_objConnection.openOutputStream());
String objName = null;
String strGameDiff = null;
String strGameLevel = null;
String strScore = null;
byte bytGameLevel;
long lScore;
byte bytGameDiff;
byte[] arrTempNameNScroes = null;
byte[] arrAllNameNScroes = new byte[400];
int nIndex = 0; // The last used index of arrAllNameNScroes
int nNumRecords = 0; // Number of records being uploaded.
for (int nRecordIndex = 0;
((nRecordIndex < m_objErixMIDlet.m_arrHighScores.length)
&& (m_nHighScoreStatus != UPLOAD_CANCELLED));
nRecordIndex++) {
// Collect all non-empty records.
//System.out.println("nRecordIndex = " + nRecordIndex);
// Process this record only if it has a non-zero score.
if (m_objErixMIDlet.m_arrHighScores[nRecordIndex] != 0) {
// Appending the key strings to their values
objName = m_objErixMIDlet.m_arrHighScoreNames[nRecordIndex].toString();
objName = "n" + nRecordIndex + "=".concat(objName);
bytGameDiff = m_objErixMIDlet.m_arrDiffLevelHighScores[nRecordIndex];
// In this game, the numerical values
// of the various difficulty levels are:
// 0 = Easy, 1 = Medium, 2 = Hard.
// However, the Highscores server
// expects the following values:
// 0 = Very Easy, 1 = Easy, 2 = Medium,
// 3 = Hard, 4 = Very Hard.
// Therefore, while sending the highscores data
// to the server, we adjust the values to be sent
// for the difficulty levels.
bytGameDiff++; // To adjust value to that expect by server.
strGameDiff = (new Byte(bytGameDiff)).toString();
strGameDiff = "d" + nRecordIndex + "=".concat(strGameDiff);
bytGameLevel = m_objErixMIDlet.m_arrLevelForHighScores[nRecordIndex];
strGameLevel = (new Byte(bytGameLevel)).toString();
strGameLevel = "l" + nRecordIndex + "=".concat(strGameLevel);
lScore = m_objErixMIDlet.m_arrHighScores[nRecordIndex];
strScore = Long.toString(lScore);
strScore = "s" + nRecordIndex + "=".concat(strScore);
// Call to method that returns one set of high score record delimited by line feed
arrTempNameNScroes = getNameScoreByteArray(objName,strGameDiff,strGameLevel,strScore);
//System.out.println("Name and score at index " + nRecordIndex + " = " + new String(arrTempNameNScroes, "UTF-8"));
// Appending one set of scores to the larger array
for (int j = 0; j< arrTempNameNScroes.length; j++ ) {
arrAllNameNScroes[nIndex++] = arrTempNameNScroes[j];
}
// Count one more highscore record being uploaded
nNumRecords++;
} // end of if the current record has a non-zero score.
} // end of loop that iterates through all high-score entries.
//System.out.println("All names n scores = " + new String(arrAllNameNScroes, "UTF-8"));
// Call to the method which concatenates the message
// header with all the High scores data and the authentication key.
byte[] arrdataToSend = buildString(arrAllNameNScroes, nIndex, nNumRecords);
/*
// Temp code to get data dump - Begin
strDataSent = new String(arrdataToSend, "UTF-8");
strbufPrintableData = new StringBuffer(strDataSent.length() * 2);
int nBufIndex = 0;
for (int i=0; i<strDataSent.length(); i++) {
// If current character of the String is a newline ...
if (strDataSent.charAt(i) == '\f') {
// Copy it to the buffer as two separate characters
strbufPrintableData.append("\\");
strbufPrintableData.append("f");
}
// else if the current character of the String is a null (0x00) ...
else if (strDataSent.charAt(i) == '\0') {
// Copy it to the buffer as two separate characters
strbufPrintableData.append('\\');
strbufPrintableData.append('0');
}
// Copy all other characters as-such
else {
strbufPrintableData.append(strDataSent.charAt(i));
}
} // end for-loop
//System.out.println("String representation of data sent: ");
//System.out.println(strDataSent);
//System.out.println("Length of String representation of data sent: " + strDataSent.length());
System.out.println("StringBuffer representation of data sent: ");
System.out.println(strbufPrintableData.toString());
System.out.println("Length of StringBuffer representation of data sent: " + strbufPrintableData.length());
// Temp code to get data dump - End
*/
// If the user has cancelled the upload operation,
// do not proceed further.
if (m_nHighScoreStatus == UPLOAD_CANCELLED) {
m_objErixMIDlet.bytHTTPStatus = m_objErixMIDlet.NOT_UPLOADED;
m_objErixMIDlet.m_bSendMenu = true;
return UPLOAD_CANCELLED;
}
// Writing the data on the connection stream
// objDataOutputStream.write(arrdataToSend);
objDataOutputStream.writeUTF(new String(arrdataToSend, "UTF-8"));
// Close the data output stream (flushes automatically)
objDataOutputStream.close();
// Discard the compiled data.
m_strFullString = null;
arrdataToSend = null;
// If the user has cancelled the upload operation,
// do not proceed further.
if (m_nHighScoreStatus == UPLOAD_CANCELLED) {
m_objErixMIDlet.bytHTTPStatus = m_objErixMIDlet.NOT_UPLOADED;
m_objErixMIDlet.m_bSendMenu = true;
return UPLOAD_CANCELLED;
}
// Open an InputStream to get response from the server.
objInputStream = m_objConnection.openInputStream();
nHTTPResponseCode = m_objConnection.getResponseCode();
// If the user has cancelled the upload operation,
// do not proceed further.
if (m_nHighScoreStatus == UPLOAD_CANCELLED) {
m_objErixMIDlet.bytHTTPStatus = m_objErixMIDlet.NOT_UPLOADED;
m_objErixMIDlet.m_bSendMenu = true;
return UPLOAD_CANCELLED;
}
// Get the content of the server response
getServerresponse(m_objConnection, objInputStream, nHTTPResponseCode);
// Save the serial number returned by the server
//saveSerialNumber() is called in getServerresponse() - keshav 17 Jun 2003
//m_objErixMIDlet.saveSerialNumber();
// Save the UI parameters returned by the server, into the RMS
m_objErixMIDlet.saveAllUploadUiParameters(nHTTPResponseCode);
// Determine the code to be returned to the calling method.
if (nHTTPResponseCode == HttpConnection.HTTP_OK) {
//System.out.println("UPLOAD_SUCCESSFUL");
// The upload operation was sucessful.
nHTTPResponseCode = UPLOAD_SUCCESSFUL;
m_objErixMIDlet.bytHTTPStatus = m_objErixMIDlet.UPLOADED;
// Record that the most recent upload operation
// (this one) has succeeded.
m_objErixMIDlet.saveHTTPStatus(m_objErixMIDlet.UPLOADED);
// Since the upload operation was sucessful, there is no
// need to make the "Send" command available until
// the next time a highscore is achieved.
m_objErixMIDlet.m_bSendMenu = false;
} else if (nHTTPResponseCode == HttpConnection.HTTP_UNAUTHORIZED) {
//System.out.println("UPLOAD_FAILED_AUTHENTICATION_ERROR");
// The upload operation failed
// due to an authentication error.
nHTTPResponseCode = UPLOAD_FAILED_AUTHENTICATION_ERROR;
m_objErixMIDlet.bytHTTPStatus = m_objErixMIDlet.NOT_UPLOADED;
// Record that the most recent upload operation
// (this one) has failed.
m_objErixMIDlet.saveHTTPStatus(m_objErixMIDlet.NOT_UPLOADED);
// Since the upload operation was a failure, there is a
// need to make the "Send" command available the next
// time a highscore is achieved.
m_objErixMIDlet.m_bSendMenu = true;
} else {
//System.out.println("UPLOAD_FAILED_OTHER_ERROR");
// The upload operation failed due to an error
// other than an authentication error.
nHTTPResponseCode = UPLOAD_FAILED_OTHER_ERROR;
m_objErixMIDlet.bytHTTPStatus = m_objErixMIDlet.NOT_UPLOADED;
// Record that the most recent upload operation
// (this one) has failed.
m_objErixMIDlet.saveHTTPStatus(m_objErixMIDlet.NOT_UPLOADED);
// Since the upload operation was a failure, there is a
// need to make the "Send" command available the next
// time a highscore is achieved.
m_objErixMIDlet.m_bSendMenu = true;
}
} // end of inner try block in which data is compiled and sent.
catch (Exception e) {
System.out.println("HST: inner catch block: " + e);
// If the exception occured due to the user cancelling the
// upload midway (resulting in the connection being closed),
// then return the error code to indicte this, otherwise
// return a general error code.
if (m_nHighScoreStatus == UPLOAD_CANCELLED) {
nHTTPResponseCode = UPLOAD_CANCELLED;
} else {
nHTTPResponseCode = UPLOAD_FAILED_OTHER_ERROR;
}
m_objErixMIDlet.bytHTTPStatus = m_objErixMIDlet.NOT_UPLOADED;
// Record that the most recent upload operation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -