使用nameValuePairs在Android中通过HTTP发布整数值

deu*_*on0 1 android http-post

我试图将游戏高分发布到我的数据库表,但我无法弄清楚如何通过传递整数值,只传递字符串.以下是我想在游戏结束时调用的方法,这应该将分数插入到我的表的分数列中.

    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
    }
} 
Run Code Online (Sandbox Code Playgroud)

我在参数"得分"下得到一条红线,因为它告诉我它只能取一个字符串,它知道最终得分是一个整数.

如果我能解决这个问题,那么我将创建一种方法,用户可以通过表单输入输入名称,并将其与分数一起输入到表格的用户列中.我已经通过Stackoverflow搜索整数和nameValuePairs的帮助,但找不到任何东西.

我感谢任何帮助.

谢谢.

Arg*_*yle 7

URL编码的表单是文本.您必须将整数分数转换为文本并将其解析回服务器端的整数.或者,你可以设计一些类型的游戏状态数据和POST的二进制文件,但如果你只想要一个名字和一个分数,这是严重的过度杀伤.

  • 感谢您使用此技巧并且它有效:String s = new Integer(finalscore).toString(); 我没想过你没有提到它.谢谢. (2认同)