1

I am trying to post a game high-score to my database table, but I cannot figure out how to do this by passing an integer value, only strings. The following is the method I want to call when the game ends, this should insert the score into the score column of my table.

    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
    }
} 

I am getting a red line under the parameters "score", finalscore as it tells me it can only take a string, and it knows that final score is an integer.

If I can figure this out, I will then create a way where the user can enter a name via a form input and this will be entered into the user column of the table along with the score. I have searched through Stackoverflow for help with integers and nameValuePairs but could not find anything.

I appreciate any assistance.

Thanks.

1
  • You haven't mentioned any particular error you've encountered with the code you've posted. What's not working the way you want it to? Commented Mar 5, 2012 at 22:16

1 Answer 1

7

URL-encoded forms are text. You will necessarily have to convert your integer scores into text and parse them back into integers on the server side. Alternately, you can devise some kind if binary file for game state data and POST that, but that's serious overkill if you just want a name and a score.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the tip I used this and it worked: String s = new Integer(finalscore).toString(); I wouldn't have thought about it you hadn't mentioned it. Thanks.
Glad to help. Try Integer.toString(finalscore) if you want to save yourself an object instantiation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.