http post 方法传递参数的2种方式
2013-10-12 18:16
381 查看
try{ HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(param);//param参数,可以为"key1=value1&key2=value2"的一串字符串 stringEntity.setContentType("application/x-www-form-urlencoded"); httpPost.setEntity(stringEntity); HttpClient client = new DefaultHttpClient(); HttpResponse httpResponse = client.execute(httpPost); String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8); } catch(IOException e){ }
以android为例吧,有的时候我们不想要通过下面的方式来传递参数,因为考虑ndk的接口时我比较喜欢的方式是直接把key和value连成一串,如"key1=value1&key2=value2"来作为参数,这样http get和post的方法都可以用同样的结构来作为参数,于是http post的方法请求服务器数据时可以用上面的方法来实现.
List<NameValuePair>list = new ArrayList<NameValuePair>(); for (int i = 0; i < keys.length; i++) { list.add(new BasicNameValuePair(keys[i], values[i])); } HttpPost httpRequst = new HttpPost(urlString); httpRequst.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));
httpRequst.setEntity()这个方法是最主要的post传递的参数实现的方式了(不知道这样说对不对)
httpEntity有AbstractHttpEntity, BasicHttpEntity, BasicManagedEntity, BufferedHttpEntity, ByteArrayEntity, EntityTemplate, FileEntity, HttpEntityWrapper, InputStreamEntity,SerializableEntity, StringEntity, UrlEncodedFormEntity
相关文章推荐
- http post 方法传递参数的2种方式
- 方法的参数传递方式
- Java方法的参数传递方式为: 值传递
- 【转】java方法参数传递方式--按值传递、引用传递
- java方法参数传递方式
- 方法传递参数的方式
- 调用对方WSDL中方法,使用CALL方式传递字符串参数
- Java 方法参数传递方式
- java方法参数传递方式--按值传递、引用传递
- 超连接的方式给应用程序传递参数方法的实现
- 关于"Java中方法参数的传递方式"的探讨
- 《Java中方法的参数传递方式只有一种:值传递》
- 新手容易混乱的String+和StringBuffer,以及Java的方法参数传递方式。
- c#方法中调用参数的值传递方式和引用传递方式,以及ref与out的区别
- flex给服务器端传递参数的2种方式
- Java中方法调用参数传递的方式是传值,尽管传的是引用的值而不是对象的值。(Does Java pass by reference or pass by value?)
- c#方法中调用参数的值传递方式和引用传递方式,以及ref与out的区别
- 读<<CLR via C#>>总结(7) 以传引用的方式向方法传递参数---ref&out
- 新手容易混乱的String+和StringBuffer,以及Java的方法参数传递方式。
- 关于"Java中方法参数的传递方式"的探讨