安卓网络(三)Volley(注:Volley是Google 2013年的 I/O大会 上,发布了的一个框架)
这篇我们先说HttpClient.
HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。
首先要学这个,肯定要先知道相关API和使用。常用的API有:
1.HttpClient/DefaultHttpClient :能提交请求的客户端对象
•HttpResponse execute (HttpUriRequest request)执行包含请求数据的请求对象, 返回包含响应数据的响应对象
•HttpParamsgetParams()得到包含请求参数的对象
2.: HttpConnectionParams :设置请求参数的工具类
•staticsetConnectionTimeout(params, time) :设置获取连接的超时时间
•staticsetSoTimeout(params, time):设置读取数据的超时时间
3.HttpGet :Get请求
HttpGet(String path) : 包含请求路径的构造方法
4.HttpPost :Post请求
•HttpPost(String path) : 包含请求路径的构造方法
setEntity(HttpEntity entity) : 设置请求体
使用方法:
使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。
1. 创建HttpClient对象。
2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用 setEntity(HttpEntity entity)方法来设置请求参数。
4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity 对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
6. 释放连接。无论执行方法是否成功,都必须释放连接
代码:
public void testClientGet(View v) {
//1. 显示ProgressDialog
final ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中...");
//2. 启动分线程
new Thread(){
//3. 在分线程, 发送请求, 得到响应数据
public void run() {
try {
//1). 得到path, 并带上参数name=Tom1&age=11
String path = et_network_url.getText().toString()+"?name=Tom3&age=13";
//2). 创建HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
//3). 设置超时
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
//4). 创建请求对象
HttpGet request = new HttpGet(path);
//5). 执行请求对象, 得到响应对象
HttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode==200) {
//6). 得到响应体文本
HttpEntity entity = response.getEntity();
final String result = EntityUtils.toString(entity);
//4. 要主线程, 显示数据, 移除dialog
runOnUiThread(new Runnable() {
@Override
public void run() {
et_network_result.setText(result);
dialog.dismiss();
}
});
}
//7). 断开连接
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
e.printStackTrace();
//如果出了异常要移除dialog
dialog.dismiss();
}
}
}.start();
}
* 使用httpClient提交post请求
*/
public void testClientPost(View v) {
//1. 显示ProgressDialog
final ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中...");
//2. 启动分线程
new Thread(){
//3. 在分线程, 发送请求, 得到响应数据
public void run() {
try {
//1). 得到path
String path = et_network_url.getText().toString();
//2). 创建HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
//3). 设置超时
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
//4). 创建请求对象
HttpPost request = new HttpPost(path);
//设置请求体
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("name", "Tom4"));
parameters.add(new BasicNameValuePair("age", "14"));
HttpEntity entity = new UrlEncodedFormEntity(parameters);
request.setEntity(entity);
//5). 执行请求对象, 得到响应对象
HttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode==200) {
//6). 得到响应体文本
entity = response.getEntity();
final String result = EntityUtils.toString(entity);
//4. 要主线程, 显示数据, 移除dialog
runOnUiThread(new Runnable() {
@Override
public void run() {
et_network_result.setText(result);
dialog.dismiss();
}
});
}
//7). 断开连接
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
e.printStackTrace();
//如果出了异常要移除dialog
dialog.dismiss();
}
}
}.start();
}