您的位置:首页 > 理论基础 > 计算机网络

android网络编程——HttpGet、HttpPost比较

2015-05-27 20:36 405 查看

在Android SDK中提供了Apache HttpClient(org.apache.http.*)模块。在这个模块中涉及到两个重要的类:HttpGet和HttpPost,他们有共性也有不同。

HttpGet和HttpPost创建方式相同:

1、创建HttpGet(或HttpPost)对象,将要请求的URL通过构造方法传入HttpGet(或HttpPost)对象中;
2、使用DefaultHttpClient类的execute方法发送HTTP GET或HTTP POST 请求,并返回HttpResponse对象;
3、通过HttpResponse接口的getEntity方法返回响应信息。

HttpGet和HttpPost不同点,HttpPost在使用是需要传递参数
,使用List<NameValuePair>添加参数。



[java] view
plaincopyprint?

<span style="font-weight: normal;"> List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

postParameters.add(new BasicNameValuePair("username", "test"));

postParameters.add(new BasicNameValuePair("password", "test1234"));</span>

/**

* @author 张兴业

* 邮箱:xy-zhang#163.com

* android开发进阶群:278401545

*

*/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: