我想向我的數據庫表發佈遊戲高分,但我無法弄清楚如何通過傳遞整數值,只有字符串來做到這一點。以下是我在遊戲結束時要調用的方法,這應該將分數插入到我的表的分數列中。在Android上使用nameValuePairs通過HTTP發佈整數值
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://deucalion0.co.uk/insert.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("score", finalscore)); //"score" is the name of the database table, finalscore is the integer that contains the value
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
我正在下的參數「分數」一條紅線,finalscore因爲它告訴我,它只能拿一個字符串,它知道最後的比分是一個整數。
如果我能想出來,我將創建一種方法,用戶可以通過表單輸入輸入名稱,並將其與分數一起輸入表格的用戶列。我已經通過Stackoverflow搜索整數和nameValuePairs的幫助,但找不到任何東西。
我很感激任何幫助。
謝謝。
你還沒有提到你所發佈的代碼遇到的任何特定錯誤。什麼不按照你想要的方式工作? – Argyle 2012-03-05 22:16:32