android如何发送int和double作为namevaluepair作为http post

android如何发送int和double作为namevaluepair作为http post,android,web-services,http-post,httpclient,Android,Web Services,Http Post,Httpclient,我有一个应用程序,我正在尝试使用http post将数据发送到Web服务。用户数据是字符串int和double的混合体。在应用程序中,所有参数都表示为字符串,就像我使用AsyncTask运行网络调用时一样(因此它不在主线程上),params数组的类型为String 我遇到的问题是服务器有时需要int。例如compID是服务器期望的int。使用http post时,我使用NameValuePair。这将只接受字符串。如何向http post传递int或double 在我的活动中 String[]

我有一个应用程序,我正在尝试使用http post将数据发送到Web服务。用户数据是字符串int和double的混合体。在应用程序中,所有参数都表示为字符串,就像我使用AsyncTask运行网络调用时一样(因此它不在主线程上),params数组的类型为String

我遇到的问题是服务器有时需要int。例如compID是服务器期望的int。使用http post时,我使用NameValuePair。这将只接受字符串。如何向http post传递int或double

在我的活动中

String[] params = new String[]{tagCompany, tagId, tagPerson, OUT,
                                null, null,null, null, null, null}; 
                        AsyncPostData apd = new AsyncPostData();
                        apd.execute(params);





private class AsyncPostData extends AsyncTask<String, Void, Void> {

        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute()
        {
            progressDialog= ProgressDialog.show(NfcscannerActivity.this, 
                    "Connecting to Server"," Posting data...", true);            
        };  


        @Override
        protected Void doInBackground(String... params) {

            nfcscannerapplication.loginWebservice.postData(params[0], params[1], params[2], params[3], params[4],
                    params[5], params[6], params[7], params[8], params[9]);
            return null;
        }

         @Override
            protected void onPostExecute(Void result)
            {
             super.onPostExecute(result);
                if(progressDialog != null)
                progressDialog.dismiss();

            }
    }//end of AsyncPostData 
String[]params=new String[]{tagCompany,tagId,tagPerson,OUT,
null,null,null,null,null,null,null};
AsyncPostData apd=新建AsyncPostData();
apd.execute(参数);
私有类AsyncPostData扩展AsyncTask{
进行对话进行对话;
@凌驾
受保护的void onPreExecute()
{
progressDialog=progressDialog.show(NFCSCanneActivity.this,
“连接到服务器”、“发布数据…”,true);
};  
@凌驾
受保护的Void doInBackground(字符串…参数){
nfcscannerapplication.loginWebservice.postData(参数[0],参数[1],参数[2],参数[3],参数[4],
参数[5]、参数[6]、参数[7]、参数[8]、参数[9];
返回null;
}
@凌驾
受保护的void onPostExecute(void结果)
{
super.onPostExecute(结果);
如果(progressDialog!=null)
progressDialog.disclose();
}
}//AsyncPostData结束

我的post方法

public void postData( String compID, String tagID, String clientID, String carerID, 
            String phoneScanned, String phoneSent, String TXType, String phoneType, String latitude, String longitude) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://cfweb.yourofficeanywhere.co.uk:88/roadrunner.asmx/PostTransaction");


        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("CompanyID", compID));
            nameValuePairs.add(new BasicNameValuePair("TagID", tagID));
            nameValuePairs.add(new BasicNameValuePair("ClientID", clientID));
            nameValuePairs.add(new BasicNameValuePair("CarerID", carerID));
            nameValuePairs.add(new BasicNameValuePair("PhoneScanned", "2010-10-16 16:30 000"));
            nameValuePairs.add(new BasicNameValuePair("PhoneSent", "2010-10-16 16:32 000"));
            nameValuePairs.add(new BasicNameValuePair("TXType", "2"));
            nameValuePairs.add(new BasicNameValuePair("PhoneType", "2"));
            nameValuePairs.add(new BasicNameValuePair("Latitude", latitude));
            nameValuePairs.add(new BasicNameValuePair("Longitude", longitude));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            Log.e(TAG, "response of post = " + response.toString());



        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    } 
public void postData(字符串compID、字符串tagID、字符串clientID、字符串carerID、,
字符串电话扫描、字符串电话发送、字符串TXType、字符串电话类型、字符串纬度、字符串经度){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://cfweb.yourofficeanywhere.co.uk:88/roadrunner.asmx/PostTransaction");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“CompanyID”,compID));
添加(新的BasicNameValuePair(“TagID”,TagID));
添加(新的BasicNameValuePair(“ClientID”,ClientID));
添加(新的BasicNameValuePair(“CarerID”,CarerID));
添加(新的基本名称对(“电话扫描”,“2010-10-16 16:30000”);
添加(新的BasicNameValuePair(“PhoneSend”,“2010-10-16 16:32 000”);
添加(新的BasicNameValuePair(“TXType”,“2”));
添加(新的BasicNameValuePair(“PhoneType”,“2”));
添加(新的BasicNameValuePair(“纬度”,纬度));
添加(新的BasicNameValuePair(“经度”,经度));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
Log.e(标记“response of post=“+response.toString());
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
} 

在我的post方法中,一些值现在设置为null。我想知道的是如何在NameValuePair中将compID设置为int。compID作为字符串从活动中返回,但服务器需要一个int。

只需将字符串解析为如下所示的整数即可

int result = Integer.parseInt(compID);
但您必须确保compID可以解析为整数。如果它包含的符号不是数字,则此操作将失败。为了确保compID是可解析的,您可以使用

if(compID.matches("(\\d+)"){
    result = Integer.parseInt(compId); }

您可以这样做:

nameValuePairs.add(new BasicNameValuePair("TXType",Integer.toString (2)));

TxType是键,2是值

这个问题已经问了很久了,但是我想帮助那些登陆到这个页面的人。在上面的参数中,你不能在namevaluepair中使用double、long或int,但你可以使用JSONObject,它可以成功地处理这些原语,而namevaluepair的请求响应不好,我在下面写了一个示例

 JSONObject juo = new JSONObject();
    juo.put("City", txtCity.getText().toString().trim().toString());
    juo.put("ClassNumber",
            Long.parseLong(txtClassNumber.getText().toString()
                    .trim()));
    juo.put("EmailAddress", txtEmail.getText().toString().trim()
            .toString());
    juo.put("ID", Long.parseLong(userid));
    juo.put("RealName", txtRealName.getText().toString().trim());
    juo.put("RealSurname", txtRealSurname.getText().toString()
            .trim());
    juo.put("School", txtSchool.getText().toString().trim());
    juo.put("UserId", Long.parseLong(userid));
    juo.put("UserName", username);
    juo.put("UserProfileImage", profileImageUrl);

    StringEntity entityForPost = new StringEntity(juo.toString());
    hpost.setHeader("content-type", "application/json");
    // hpost.setEntity(new UrlEncodedFormEntity(UpdateParams));
    hpost.setEntity(entityForPost);

    HttpResponse hres = hclient.execute(hpost);

    StatusLine status = hres.getStatusLine(); 

嗨,这就是我的问题。如果我转到nameValuePairs.add(新的BasicNameValuePairs(“CompanyID”,Integer.parseInt(compID));Eclipse抱怨NameValuePairTry nameValuePairs.add(新的BasicNameValuePair(“CompanyID”),((int)Integer.parseInt(compID))的字符串int未定义;BasicNameValuePair的构造函数是(String,String),所以它不起作用。所以。。。您的问题是服务器只接受int,但从应用程序只能发送字符串?是的,Astrorvald做对了。构造函数是字符串。我需要发送字符串,int到服务器。除了NameValePair之外,还有其他数据结构可以处理这个问题吗?嗨,变量compID是一个字符串。NameValuePair只接受not,但服务器希望您可以在服务器中更改数据类型(如果可能)。我想这样就不那么乏味了。我和导演聊过了,他说我们也许可以改变服务器的期望。就像你说的可能更容易:)