android HttpPost传参数的一些总结
1,传递String类型参数
//封装post请求的数组
List formparams = new ArrayList();
formparams.add(new BasicNameValuePair("param1", "value1"));//参数使用BasicNameValuePair封装
formparams.add(new BasicNameValuePair("param2", "value2"));
//UrlEncodedFormEntity这个类是用来把输入数据编码成合适的内容
//实际编码后变成这样的格式:param1=value1¶m2=value2
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httppost = new HttpPost("http://localhost/handler.do");
//post请求设置参数
httppost.setEntity(entity);
2,传递字节数组的参数
使用ByteArrayEntity封装字节
//字节信息
byte[] byte_lon = StringUtils.getBytes(lon);
byte[] byte_lat = StringUtils.getBytes(lat);
//使用ByteBuffer缓冲器,构建一个字节数为10的缓冲字节
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put(byte_lon);
buffer.put(byte_lat);
bytep[] params=buffer.array();
//实例化字节数组实体
ByteArrayEntity arrayEntity1 = new ByteArrayEntity(bytelonlat);
arrayEntity1.setContentType("application/octet-stream");
httpPost.setEntity(params);