2011-12-22 77 views
5

发送整数到HTTP服务器我想从我的Android客户端使用该NameValuePair方法的几个值发送到Web服务器:使用的NameValuePair

public void postData() { 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http:/xxxxxxx"); 

     try { 
      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
      String amount = paymentAmount.getText().toString(); 
      String email = inputEmail.getText().toString(); 
      nameValuePairs.add(new BasicNameValuePair("donationAmount", amount)); 
      nameValuePairs.add(new BasicNameValuePair("email", email)); 
      nameValuePairs.add(new BasicNameValuePair("paymentMethod", "5")); 
      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 
     } 
    } 

可惜NameValuePair只能够发送字符串,我需要也发送整数值。任何人都可以帮我解决我的问题吗?

回答

13

嗨hectichavana如果你想使用的名称值对,你可以尝试这样的

nameValuePairs.add(new BasicNameValuePair("gender",Integer.toString(1))); 

其中性别代表键和1将成为该键的值发送整数值。 希望得到这个帮助。

+0

我有与象不同势名称微调五个值,B,C,d,E,连接要通过这串values..how的整数值做呢?<字符串数组名=“planets_array”> <项目值= “1”>一个 <项目值= “2”> b <项目值= “3”>ç <项目值= “4”> d <项目值=“5 “e f 2014-11-04 12:10:30

+0

@AdityaVyas,在新帖子中提出一个新问题。 – 2014-11-17 00:06:19

+0

它只是从微调获取选定值的问题。然后你可以使用Integer.toString()方法将Integer转换为String。 @DarylBennett是正确的你的问题是不相关的这个线程。 – 2014-11-17 05:41:35

1

道歉,如果我陈述明显和/或缺少点,但自然的解决方案似乎是将整数转换为字符串,然后转换回服务器端。更完整的解决方案是使用不同的表示(如XML)来编码数据。

2

我不认为您的帖子的另一端请求关心您的值格式客户端使用并将其全部作为字符串。所以IMO这就是为什么NameValuePair只采用String。 如果你的数据是数字格式,你可以随时将其转换回字符串并使用的NameValuePair

new BasicNameValuePair("integer", new Integer().toString(value)); 

配对是一个例子,我经常使用。

相关问题