2012-03-05 79 views
1

我想向我的数据库表发布游戏高分,但我无法弄清楚如何通过传递整数值,只有字符串来做到这一点。以下是我在游戏结束时要调用的方法,这应该将分数插入到我的表的分数列中。在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的帮助,但找不到任何东西。

我很感激任何帮助。

谢谢。

+0

你还没有提到你所发布的代码遇到的任何特定错误。什么不按照你想要的方式工作? – Argyle 2012-03-05 22:16:32

回答

7

URL编码形式是文本。您必须将您的整数分数转换为文本并将其解析回服务器端的整数。或者,你可以设计一些用于游戏状态数据和POST的二进制文件,但如果你只是想要一个名字和一个分数,那就是严重的矫枉过正。

+0

啊,所以我应该将整数值转换为一个字符串,我会尝试! 谢谢。 – deucalion0 2012-03-05 22:21:45

+2

感谢您使用这个提示,它的工作原理如下: String s = new Integer(finalscore).toString(); 我不会考虑它,你没有提到它。 谢谢。 – deucalion0 2012-03-05 23:30:05

+1

很高兴提供帮助。如果你想为自己保存一个对象实例,试试'Integer.toString(finalscore)'。 – Argyle 2012-03-05 23:53:47