基础学习总结(八)--HttpClient

在Android开发中,Android SDK附带了Apache的HttpClient,它是一个完善的客户端。它提供了对HTTP协议的全面支持,可以使用HttpClient的对象来执行HTTP GET和HTTP POST调用。

HTTP工作原理:

1.客户端(一般是指浏览器,这里是指自己写的程序)与服务器建立连接

2.建立连接后,客户端向

服务器发送请求

3.服务器接收到请求后,向客户端发送响应信息

4.客户端与服务器断开连接

HttpClient的一般使用步骤:

1.使用DefaultHttpClient类实例化HttpClient对象

2.创建HttpGet或HttpPost对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象。

3.调用execute方法发送HTTP GET或HTTP POST请求,并返回HttpResponse对象。

4.通过HttpResponse接口的getEntity方法返回响应信息,并进行相应的处理。

最后记得要在AndroidManifest.xml文件添加网络权限

<uses-permission android:name="android.permission.INTERNET" />

1.使用HttpClient来执行GET调用

 1 public static String loginOfGet(String userName, String password) {
 2         HttpClient client = null;
 3         try {
 4             // 定义一个客户端
 5             client = new DefaultHttpClient();
 6             
 7             // 定义一个get请求方法
 8             String data = "username=" + userName + "&password=" + password;
 9             HttpGet get = new HttpGet("http://10.0.2.2:8080/ServerItheima28/servlet/LoginServlet?" + data);
10             
11             // response 服务器相应对象, 其中包含了状态信息和服务器返回的数据
12             HttpResponse response = client.execute(get);    // 开始执行get方法, 请求网络
13             
14             // 获得响应码
15             int statusCode = response.getStatusLine().getStatusCode();
16             
17             if(statusCode == 200) {
18                 InputStream is = response.getEntity().getContent();
19                 String text = getStringFromInputStream(is);
20                 return text;
21             } else {
22                 Log.i(TAG, "请求失败: " + statusCode);
23             }
24         } catch (Exception e) {
25             e.printStackTrace();
26         } finally {
27             if(client != null) {
28                 client.getConnectionManager().shutdown();    // 关闭连接, 和释放资源
29             }
30         }
31         return null;
32     }
GET提交

2.使用HttpClient来执行POST调用

 1 public static String loginOfPost(String userName, String password) {
 2         HttpClient client = null;
 3         try {
 4             // 定义一个客户端
 5             client = new DefaultHttpClient();
 6             
 7             // 定义post方法
 8             HttpPost post = new HttpPost("http://10.0.2.2:8080/ServerItheima28/servlet/LoginServlet");
 9             
10             // 定义post请求的参数
11             List<NameValuePair> parameters = new ArrayList<NameValuePair>();
12             parameters.add(new BasicNameValuePair("username", userName));
13             parameters.add(new BasicNameValuePair("password", password));
14             
15             // 把post请求的参数包装了一层.
16             
17             // 不写编码名称服务器收数据时乱码. 需要指定字符集为utf-8
18             UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
19             // 设置参数.
20             post.setEntity(entity);
21             
22             // 设置请求头消息
23 //            post.addHeader("Content-Length", "20");
24             
25             // 使用客户端执行post方法
26             HttpResponse response = client.execute(post);    // 开始执行post请求, 会返回给我们一个HttpResponse对象
27             
28             // 使用响应对象, 获得状态码, 处理内容
29             int statusCode = response.getStatusLine().getStatusCode();    // 获得状态码
30             if(statusCode == 200) {
31                 // 使用响应对象获得实体, 获得输入流
32                 InputStream is = response.getEntity().getContent();
33                 String text = getStringFromInputStream(is);
34                 return text;
35             } else {
36                 Log.i(TAG, "请求失败: " + statusCode);
37             }
38         } catch (Exception e) {
39             e.printStackTrace();
40         } finally {
41             if(client != null) {
42                 client.getConnectionManager().shutdown();    // 关闭连接和释放资源
43             }
44         }
45         return null;
46     }
POST提交
原文地址:https://www.cnblogs.com/cuijl/p/4601111.html