使用NameValuePair将整数发送到HTTP Server

hec*_*ana 5 android

我想使用此NameValuePair方法从我的Android客户端向Web服务器发送几个值:

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

不幸的NameValuePair是只能发送String,我也需要发送Integer值.有人可以帮我解决我的问题吗?

Ros*_*Jha 13

您好hectichavana如果您想使用名称值对发送整数值,您可以尝试这样

nameValuePairs.add(new BasicNameValuePair("gender",Integer.toString(1)));
Run Code Online (Sandbox Code Playgroud)

性别代表密钥,1代表这个密钥的价值.希望这有帮助.