deucalion0
deucalion0

Reputation: 2440

Post an integer value over HTTP in Android using nameValuePairs

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.

Upvotes: 1

Views: 6368

Answers (1)

Argyle
Argyle

Reputation: 3394

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.

Upvotes: 7

Related Questions