日期:2014-05-17  浏览次数:20650 次

java post时设置参数的问题求助
用HttpPost方式post参数时,我用了BasicNameValuePair类添加参数,但是BasicNameValuePair类的参数(key/value)只支持String格式,如果想传递int参数,BasicNameValuePair就会报错。我的代码如下:

? int id = 12345;
? String url = "https://somedomain.com/create.json";
? HttpPost httpPost = new HttpPost(url);
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("access_token", accessToken));
param.add(new BasicNameValuePair("id ", id));
httpPost.setEntity(new UrlEncodedFormEntity(param,HTTP.UTF_8));

HttpResponse resp = new DefaultHttpClient().execute(httpPost);
String updateResult ="";
updateResult = EntityUtils.toString(resp.getEntity(),"utf-8");
System.out.println("评论 = "+updateResult);

上面代码中param.add(new BasicNameValuePair("id ", id));会报错,原因是BasicNameValuePair不支持int参数。
如果我想做post时想带int参数的话,应该用什么方法呢??


------解决方案--------------------
你可以把Int转成String再传....笨是笨了点,但是可以解决问题...
------解决方案--------------------
GET和POST参数只能传递String类型,要不你就做一次编码,转换为String类型,例如Base64转换下再传递